2

我有一个具有以下属性的按钮:

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(在源代码中)

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

最终产品是我得到了一个圆形的按钮。但是,由于不同Android设备的屏幕尺寸不同,这一圈的大小是不够的。我已经查看了与此类似的其他一些问题,但他们的解决方案对我没有多大帮助。如何在 Java 代码中动态更改其大小?

4

2 回答 2

6

试试这个方法

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);
    }
});

欢呼

于 2013-09-23T04:21:31.100 回答
2
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
}

希望能帮助到你。快乐编程:)

于 2013-09-23T05:22:19.153 回答