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 (src 内)

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

最終製品は、円形のボタンを取得することです. ただし、Android デバイスごとに画面サイズが異なるため、この 1 つの円のサイズでは不十分です。これに似た他のいくつかの質問を見てきましたが、それらの解決策はあまり役に立ちませんでした。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 に答える