飞度形象的ImageButton Android中飞度、形象、Android、ImageButton

2023-09-12 01:27:19 作者:乄Ru花的那一刻灿烂

我有6个ImageButton的在我的活动,我通过我的code在其中设置的图像(不使用XML)

我希望他们能够覆盖按钮面积的75%。但如果一些图像覆盖面积较少,有些太大,放不进的ImageButton。如何以编程方式调整,并告诉他们? 下面是screeen拍

下面是XML文件

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直
    机器人:layout_marginBottom =5SP
        机器人:layout_marginLeft =2SP
        机器人:layout_marginRight =5SP
        机器人:layout_marginTop =0SP>
   <的LinearLayout
        机器人:layout_height =0dp
        机器人:layout_width =match_parent
        机器人:layout_weight =1
        机器人:方向=横向>
        <的ImageButton

            机器人:layout_height =match_parent
            机器人:layout_width =0dp
            机器人:layout_weight =1
            机器人:ID =@ + ID / button_topleft
        机器人:layout_marginBottom =5SP
        机器人:layout_marginLeft =2SP
        机器人:layout_marginRight =5SP
        机器人:layout_marginTop =0SP
            />
        <的ImageButton
            机器人:layout_height =match_parent
            机器人:layout_width =0dp
            机器人:layout_weight =1
            机器人:ID =@ + ID / button_topright
    机器人:layout_marginBottom =5SP
        机器人:layout_marginLeft =2SP
        机器人:layout_marginRight =5SP
        机器人:layout_marginTop =0SP
            />
    < / LinearLayout中>
    <的LinearLayout
        机器人:layout_height =0dp
        机器人:layout_width =match_parent
        机器人:layout_weight =1
        机器人:方向=横向>

        <的ImageButton
            机器人:layout_height =match_parent
            机器人:layout_width =0dp
            机器人:layout_weight =1
            机器人:ID =@ + ID / button_repeat
    机器人:layout_marginBottom =5SP
        机器人:layout_marginLeft =2SP
        机器人:layout_marginRight =5SP
        机器人:layout_marginTop =0SP
             />

              <的ImageButton
            机器人:layout_height =match_parent
            机器人:layout_width =0dp
            机器人:layout_weight =1
            机器人:ID =@ + ID / button_next
    机器人:layout_marginBottom =5SP
        机器人:layout_marginLeft =2SP
        机器人:layout_marginRight =5SP
        机器人:layout_marginTop =0SP
             />


    < / LinearLayout中>
   <的LinearLayout
        机器人:layout_height =0dp
        机器人:layout_width =match_parent
        机器人:layout_weight =1
        机器人:方向=横向>

        <的ImageButton
            机器人:layout_height =match_parent
            机器人:layout_width =0dp
            机器人:layout_weight =1
            机器人:ID =@ + ID / button_bottomleft
    机器人:layout_marginBottom =5SP
        机器人:layout_marginLeft =2SP
        机器人:layout_marginRight =5SP
        机器人:layout_marginTop =0SP
             />
        <的ImageButton
            机器人:layout_height =match_parent
            机器人:layout_width =0dp
            机器人:layout_weight =1
            机器人:ID =@ + ID / button_bottomright
    机器人:layout_marginBottom =5SP
        机器人:layout_marginLeft =2SP
        机器人:layout_marginRight =5SP
        机器人:layout_marginTop =0SP
            />
    < / LinearLayout中>

< / LinearLayout中>
 

和的一个片段 myClass.java:

 公共无效addImageButtons()
    {
        iB_topleft =(的ImageButton)findViewById(R.id.button_topleft);
        iB_topright =(的ImageButton)findViewById(R.id.button_topright);
        iB_bottomleft =(的ImageButton)findViewById(R.id.button_bottomleft);
        iB_bottomright =(的ImageButton)findViewById(R.id.button_bottomright);
        iB_next =(的ImageButton)findViewById(R.id.button_next);
        iB_repeat =(的ImageButton)findViewById(R.id.button_repeat);
    }

    公共无效setImageNextAndRepeat()
    {

    iB_topleft .setImageResource(R.drawable.aa);
        iB_topright.setImageResource(R.drawable.bb);

    iB_bottomleft.setImageResource(R.drawable.cc);
        iB_bottomright.setImageResource(R.drawable.dd);


        iB_next.setImageResource(R.drawable.next);
        iB_repeat.setImageResource(R.drawable.repeat);
    }
 

在事先非常感谢

解决方案   

我希望他们能够覆盖按钮面积的75%。

飞度两厢车 李圣欣

使用机器人:填充=20dp(根据需要进行调整的填充)来控制多少的图像占用按钮

  

但如果一些图像覆盖面积较少,有些太大,放不进的ImageButton。如何以编程方式调整,并告诉他们?

使用一个安卓scaleType =fitCenter有Android的规模图像和安卓adjustViewBounds =真正的来让他们调整自己的界限,由于结垢。

所有这些属性都可以在每个的ImageView 在运行时code进行设置。然而,这是很容易在我看来设置和preVIEW的XML。

此外,不要使用 SP 以外的任何文字大小等,它是根据文本大小preference用户比例集,所以你的 SP 尺寸将大于您的预期,如果用户有一个大的文字设置。使用 DP 相反,它不进行缩放,但用户的文字大小preference。

下面是每个按钮应该是什么样子的一个片段:

 <的ImageButton
        机器人:ID =@ + ID / button_topleft
        机器人:layout_width =0dp
        机器人:layout_height =match_parent
        机器人:layout_marginBottom =5DP
        机器人:layout_marginLeft =2DP
        机器人:layout_marginRight =5DP
        机器人:layout_marginTop =0dp
        机器人:layout_weight =1
        机器人:adjustViewBounds =真
        机器人:填充=20dp
        机器人:scaleType =fitCenter/>
 

I have 6 ImageButton in my activity, i set images through my code in them ( not using xml)

I want them to cover 75% of the button area. but where as some images cover less area, some are too big to fit into the imageButton. How to programatically resize and show them? below is the screeen shot

below is the xml-file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_marginBottom="5sp"
        android:layout_marginLeft="2sp"
        android:layout_marginRight="5sp"
        android:layout_marginTop="0sp"     >
   <LinearLayout
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal">
        <ImageButton          

            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:id="@+id/button_topleft"
        android:layout_marginBottom="5sp"
        android:layout_marginLeft="2sp"
        android:layout_marginRight="5sp"
        android:layout_marginTop="0sp"
            />
        <ImageButton
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:id="@+id/button_topright"
    android:layout_marginBottom="5sp"
        android:layout_marginLeft="2sp"
        android:layout_marginRight="5sp"
        android:layout_marginTop="0sp"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal">

        <ImageButton
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:id="@+id/button_repeat"
    android:layout_marginBottom="5sp"
        android:layout_marginLeft="2sp"
        android:layout_marginRight="5sp"
        android:layout_marginTop="0sp"     
             />

              <ImageButton
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:id="@+id/button_next"
    android:layout_marginBottom="5sp"
        android:layout_marginLeft="2sp"
        android:layout_marginRight="5sp"
        android:layout_marginTop="0sp"     
             />


    </LinearLayout>    
   <LinearLayout
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal">

        <ImageButton
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:id="@+id/button_bottomleft"
    android:layout_marginBottom="5sp"
        android:layout_marginLeft="2sp"
        android:layout_marginRight="5sp"
        android:layout_marginTop="0sp"                                 
             />
        <ImageButton
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:id="@+id/button_bottomright"
    android:layout_marginBottom="5sp"
        android:layout_marginLeft="2sp"
        android:layout_marginRight="5sp"
        android:layout_marginTop="0sp"                  
            />        
    </LinearLayout>        

</LinearLayout>

and a snippet of myClass.java:

public void addImageButtons()
    {
        iB_topleft = (ImageButton) findViewById(R.id.button_topleft);
        iB_topright = (ImageButton) findViewById(R.id.button_topright);
        iB_bottomleft = (ImageButton) findViewById(R.id.button_bottomleft);
        iB_bottomright = (ImageButton) findViewById(R.id.button_bottomright);
        iB_next = (ImageButton) findViewById(R.id.button_next);
        iB_repeat = (ImageButton) findViewById(R.id.button_repeat);
    }

    public void setImageNextAndRepeat()
    {

    iB_topleft .setImageResource(R.drawable.aa);
        iB_topright.setImageResource(R.drawable.bb);   

    iB_bottomleft.setImageResource(R.drawable.cc);
        iB_bottomright.setImageResource(R.drawable.dd);   


        iB_next.setImageResource(R.drawable.next);
        iB_repeat.setImageResource(R.drawable.repeat);      
    }

Many Thanks in advance

解决方案

I want them to cover 75% of the button area.

Use android:padding="20dp" (adjust the padding as needed) to control how much the the image takes up on the button.

but where as some images cover less area, some are too big to fit into the imageButton. How to programatically resize and show them?

Use a android:scaleType="fitCenter" to have Android scale the images, and android:adjustViewBounds="true" to have them adjust their bounds due to scaling.

All of these attributes can be set in code on each ImageView at runtime. However, it is much easier to set and preview in xml in my opinion.

Also, do not use sp for anything other than text size, it is scaled depending on the text size preference the user sets, so your sp dimensions will be larger than your intended if the user has a "large" text setting. Use dp instead, it is not scaled but the user's text size preference.

Here's a snippet of what each button should look like:

    <ImageButton
        android:id="@+id/button_topleft"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="0dp"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:padding="20dp"
        android:scaleType="fitCenter" />