簡體   English   中英

如何在按鈕Android中更改圖標大小

[英]How to change icon size in button android

我有一個同時包含文本和圖像的按鈕。 它所取的圖像尺寸為圖像的實際尺寸。 如何更改按鈕中圖像的大小?

<Button
    android:id="@+id/button3"
    android:layout_weight="1"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:drawableTop="@drawable/home_icon"
    android:text="Settings"
    />

通過特定的測量確定地控制尺寸。 例如:

<Button
    android:id="@+id/button3"
    android:layout_weight="1"
    android:layout_height="50dp"
    android:layout_width="50dp"
    android:background="@drawable/home_icon"
    android:text="Settings"
/>

為了進一步控制按鈕的設計,請使用諸如RelativeLayout之類的容器來保存圖像和文本。 然后將onClickListener分配給容器以將其變成可點擊的按鈕。 這是一個例子:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/snazzy_button_with_image_above_text"
    android:layout_width="wrap_content"
    android:layout_height="64dp"
    android:layout_gravity="center"
    android:layout_marginLeft="1dp"
    android:background="@color/SkyBlue"
    android:paddingBottom="2dp"
    android:paddingLeft="1dp"
    android:paddingRight="1dp"
    android:paddingTop="1dp" >

<ImageView
    android:id="@+id/your_image"
    android:layout_width="50dp"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="@color/white"
    android:contentDescription="image inside button"
    android:scaleType="centerCrop" />

<TextView
    android:id="@+id/your_text"
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/your_image"
    android:layout_alignParentLeft="true"
    android:layout_gravity="center"
    android:layout_marginBottom="0dp"
    android:layout_marginTop="0dp"
    android:background="@color/translucent_black"
    android:gravity="bottom|center_horizontal"
    android:paddingTop="0dp"
    android:text="Your Button Text"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="@color/white"
    android:textSize="10sp" />

</RelativeLayout>

在代碼中,這樣分配onClickListener:

yourButton = (RelativeLayout) findViewById(R.id.snazzy_button_with_image_above_text);
yourButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View clickedButton) {
    //button click action here 
}

您應該使用ImageButton並在android:src中指定圖像,然后將android:scaletype設置為fitXY


在代碼中設置Drawable

Drawable drawable = getResources().getDrawable(R.drawable.icon);
drawable.setBounds(0, 0, (int)(drawable.getIntrinsicWidth()*0.10), 
                     (int)(drawable.getIntrinsicHeight()*0.10));
ScaleDrawable sd = new ScaleDrawable(drawable, 0, sWidth,sHeight);
Button btn = findViewbyId(R.id.btnId);
btn.setCompoundDrawables(sd.getDrawable(), null, null, null); //set drawableLeft/Right for example

設置android:background="@drawable/image_name"

並給出寬度和高度像

android:layout_height="10dp"
android:layout_height="10dp"

或者你可以嘗試使用,而不是按鈕的ImageButton因為ImageButton的比巴頓更多的圖像渲染選項,您可以參考這里

android:scaleType =“ fitXY”

同樣,wrap_content是寬度和高度的首選參數,以便OS將根據屏幕尺寸自動調整。 請參考Android-圖像按鈕比例在Android中調整ImageButton的大小

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM