簡體   English   中英

以編程方式更改android中Button的寬度

[英]Programmatically change the width of the Button in android

如何以編程方式更改Buttonwidth 我能夠改變height但寬度不會改變。 以下是我的代碼段

private void createButton(final String label)
{

    Button button = new Button(this);
    button.setText(label);


    button.setWidth(10);
    button.setHeight(100);

    button.setOnClickListener(new OnClickListener() 
    {

        @Override
        public void onClick(View v) 
        {


        }
    });
    mMenuContainer.addView(button, mMenuItemLayoutParamters);


}

但是width的的Button占用width的畫面。

button.setLayoutParams(new LinearLayout.LayoutParams(10, 100));

應該為你工作。

在線有一個很棒的例子: 檢查一下


您應該應用包含ViewViewGroup LayoutParams 也就是說,如果您的按鈕位於RelativeLayout ,則應使用RelativeLayout.LayoutParams

你可以這樣做。

LinearLayout.LayoutParams params = button.getLayoutParams();
params.width = 100;
button.setLayoutParams(params);

但是在將按鈕添加到父級之后,請確保執行此操作。 LinearLayout.LayoutParams當母體是一種用於LinearLayout ,並且當父RelativeLayout使用RelativeLayout.LayoutParams

我用這個解決了:

//ImageButton creation
final ImageButton server_icon_button=new ImageButton(getApplicationContext());
//setting background image in drawable
server_icon_button.setBackgroundResource(R.drawable.bonsai);
//After adding ImageButton to the parent i done this
server_icon_button.getLayoutParams().height=180;
server_icon_button.getLayoutParams().width=100;

我希望這有幫助。

可以做到這一點

    int width= (int)getResources().getDimension(R.dimen.button_width);
    int heigth= (int)getResources().getDimension(R.dimen.button_heigth);
    ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(width, heigth);
    button.setLayoutParams(layoutParams);

在我的情況下,我使用AppCompatImageButton(和AppCompat)。 事實證明,如果我在各種可繪制文件夾中保持密度調整圖像:drawable-ldpi,drawable-mdpi,drawable-hdpi等,那么按鈕將無法縮放。 相反,我必須為drawable-nodpi中的每個按鈕添加一個drawable 確保drawable-nodpi按鈕圖像是您需要的最大尺寸。 Android不會縮放按鈕大小; 但是,它可以縮小尺寸。 我在XML中創建了一個按鈕,如下所示:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:meter="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <android.support.v7.widget.AppCompatImageButton
        android:id="@+id/button"
        style="@style/ButtonStyle"
        android:baselineAlignBottom="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="0dp"
        android:minWidth="0dp"
        app:srcCompat="@drawable/button_selector"
        android:background="?attr/selectableItemBackgroundBorderless"
        android:contentDescription="@string/button"
        android:focusable="true">
</RelativeLayout>

<!-- styles.xml -->
<style name="ButtonStyle" parent="MyAppTheme">
    <item name="android:scaleType">centerInside</item>
    <item name="android:adjustViewBounds">true</item>
    <item name="colorControlHighlight">@color/buttonColorPressed</item>
    <item name="colorButtonNormal">@color/buttonColor</item>
</style>

<!-- v21/styles.xml -->
<style name="TargetButton" parent="MyAppTheme">
    <item name="android:scaleType">centerInside</item>
    <item name="android:adjustViewBounds">true</item>
    <item name="colorControlHighlight">@color/buttonColorPressed</item>
    <item name="colorButtonNormal">@color/buttonColor</item>
    <item name="android:elevation">@dimen/button_elevation</item>
</style>

<!-- drawable/button_selector.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/button"/>
</selector>

<!-- drawable/button.xml -->
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
      android:src="@drawable/ic_button"
      android:gravity="center" />

<!-- drawable/button_pressed.xml -->
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
      android:src="@drawable/ic_button_pressed"
      android:gravity="center" />

接下來,在onCreate中檢索按鈕時,調用adjustButtonSize (我放入我的基類):

    mButton = (AppCompatImageButton) findViewById(R.id.button);
    adjustButtonSize(mButton);

public void adjustButtonSize(AppCompatImageButton button) {
    DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
    int width = displayMetrics.widthPixels;
    int height = displayMetrics.heightPixels;
    ViewGroup.LayoutParams params = button.getLayoutParams();
    params.height = height / 10;         // 10%
    params.width = ((width * 20) / 100); // 20%
    button.setLayoutParams(params);
}

嘗試僅使用mMenuContainer.addView(button);

你可以嘗試這個kotlin代碼。

當您想要將另一個視圖的大小賦予其他視圖時,這非常有用。

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        equalToButton.post(Runnable { zeroButton.height = equalToButton.height })
    }
}
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) yourButton.getLayoutParams();
layoutParams.width = 150;
yourButton.setLayoutParams(layoutParams);

“ConstraintLayout”可以通過“layoutParams.width”更改為您的布局和設置大小。 這個案子對我有用!

暫無
暫無

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

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