簡體   English   中英

動態/以編程方式設置具有可繪制背景的按鈕的大小

[英]Set size of button with drawable background dynamically/programmatically

我有一個具有以下屬性的按鈕:

circle_normal.xml(在res / drawable中)

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="10dp"
    android:shape="oval" >

    <solid android:color="#FF6347" />

    <size
        android:height="325dp"
        android:width="325dp" />
</shape>

circle.xml(在res / drawable中)

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/circle_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/circle_normal"></item>

</selector>

activity_main.xml(在res / layout中)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Main" >

    <Button
        android:id="@+id/button_study"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@drawable/circle"
        android:gravity="center" />
</RelativeLayout>

Main.java(在src中)

buttonStudy = (Button) findViewById(R.id.button_study);

最終的結果是我得到一個圓形的按鈕。 但是,由於不同的Android設備上的屏幕尺寸不同,因此這一圓圈大小不足。 我看過其他一些與此類似的問題,但是他們的解決方案對我沒有太大幫助。 如何在Java代碼中動態更改其大小?

嘗試這種方式

final Button buttonStudy = (Button) findViewById(R.id.button_study);
buttonStudy.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        ViewGroup.LayoutParams params = buttonStudy.getLayoutParams();
        params.width = 100;//change the width size
        params.height= 100;//change the hight size
        buttonStudy.setLayoutParams(params);
    }
});

啦啦隊

btn = (Button) findViewByID(R.id.button_study);

btn.getLayoutParams().height = x;
btn.getLayoutParams().width = x;

btn.setLayoutParams(btn.getLayoutParams);

使用此代碼,您可以設置按鈕的高度和寬度。 而且,這將避免父視圖解析沖突。

另外,您可以獲取屏幕的大小,然后相應地設置按鈕的大小:-

對於API <13

Display display = getWindowManager().getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight(); //not required, just to inform

int x = (int) screenWidth/10; //replace 10 by scaling factor

btn = (Button) findViewByID(R.id.button_study);

btn.getLayoutParams().height = x;
btn.getLayoutParams().width = x;

btn.setLayoutParams(btn.getLayoutParams);

對於API> = 13

Point size = new Point();
display.getSize(size);
int screenWidth = size.x;
int screenHeight = size.y; //not required, just for info

int x = (int) screenWidth/10; //replace 10 by scaling factor
btn = (Button) findViewByID(R.id.button_study);

btn.getLayoutParams().height = x;
btn.getLayoutParams().width = x;

btn.setLayoutParams(btn.getLayoutParams);

在運行時檢查API級別,請使用

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {

         //<code> if API>=13 (HONEYCOMB_MR2 is API 13)
}

else {

         //<code> if API<13
}

希望能幫助到你。 快樂編程:)

暫無
暫無

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

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