在安卓系统中,以编程方式改变Button的宽度

45

如何在程序中动态改变 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);


}

但是 Button 的宽度占据了屏幕的宽度。

8个回答

97

这应该对你有用。

这里有一个很棒的在线示例:查看此处


你应该应用包含你的视图的 ViewGroupLayoutParams。也就是说,如果你的按钮在 RelativeLayout 中,你应该使用 RelativeLayout.LayoutParams


4
需要注意的是:你应该使用包含你的视图的ViewGroupLayoutParams。也就是说,如果你的按钮在RelativeLayout中,你应该使用RelativeLayout.LayoutParams - agamov
1
你应该将尺寸值乘以显示比例,因为构造函数需要像素而不是dp。 像这样: widthInPixels = getResources().getDisplayMetrics().density * widthInDp; - Uwe Post
1
必须遵循使用正确的ViewGroup的建议。如果您使用不正确的VG,则应用程序将崩溃,并且logcat将没有关于原因的任何信息。 - Jose_GD
1
我不明白为什么在按钮和文本视图中提供了setWIdth()和setHeight()方法,但实际上它们似乎并没有起作用,包括对我自己而言也是如此。我在这个网站上看到很多“问题:setWIdth()不起作用,答案:使用LayoutParams”的帖子。 - Androidcoder
1
对于 Kotlin 程序员来说,它应该像这样工作:button.layoutParams = LinearLayout.LayoutParams(10, 100) - Jens van de Mötter

48

你可以这样做。

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

但是请确保在将您的按钮添加到父控件后再执行此操作。当父控件是LinearLayout时,请使用LinearLayout.LayoutParams,当父控件是RelativeLayout时,请使用RelativeLayout.LayoutParams


设置宽度可以设置“px”值或“dp”值,如果我们在XML中将dp值设置为初始值,例如width="50dp",那么如何将其更改为dp? - Bay

4
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) yourButton.getLayoutParams();
layoutParams.width = 150;
yourButton.setLayoutParams(layoutParams);

“ConstraintLayout”可以通过“layoutParams.width”来改变布局并设置大小。这对我很有效!

2

可以做到这一点

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

2
我使用以下方法解决了这个问题:
//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;

我希望你能帮到您。

1
在我的情况下,我正在使用AppCompatImageButton(和AppCompat)。 事实证明,如果我将密度调整后的图像放在各种drawable文件夹中: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);
}

0
你可以尝试这段 Kotlin 代码。
当你想把一个视图的大小提供给另一个视图时,这非常方便。
class MainActivity : AppCompatActivity() {

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

        equalToButton.post(Runnable { zeroButton.height = equalToButton.height })
    }
}

0

尝试仅使用mMenuContainer.addView(button);


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接