Android-如果ViewGroup在xml中的宽度为'match_parent'/'fill_parent',那么Button的setWidth()方法无效吗?

6

我在activity_main.xml文件中定义了一个LinearLayout作为根元素。

情况1:在onCreate()方法中,我试图向这个垂直LinearLayout中添加Button,但是让我困惑的是,根据Google的API,我尝试在将Button添加到ViewGroup之前调用setWidth(20)方法,但Button占据了“match_parent”的宽度,而不是20dp。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_margin="10dp"
    android:orientation="vertical"
    android:id="@+id/first_layout">
    </LinearLayout>

//Inside onCreate() of activity..    
    LinearLayout firstLayout = (LinearLayout) findViewById(R.id.first_layout);
            Button button = new Button(this);
            button.setText(R.string.click_on_me);
            button.setWidth(20);
            firstLayout.addView(button);

案例1

案例2:当将LinearLayout的layout_width设置为“wrap_content”,并调用setWidth(20)时,它现在被认为是给定的明确宽度值,即20dp。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_margin="10dp"
    android:orientation="vertical"
    android:id="@+id/first_layout">
</LinearLayout>

//Inside onCreate() method
LinearLayout firstLayout = (LinearLayout) findViewById(R.id.first_layout);
        Button button = new Button(this);
        button.setText(R.string.click_on_me);
        button.setWidth(20);//In this case, its working
        firstLayout.addView(button);

图片描述

情况三:最终,移除了我自定义的setWidth(20)方法调用后,按钮获取了'wrap_content'宽度。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_margin="10dp"
    android:orientation="vertical"
    android:id="@+id/first_layout">

</LinearLayout>

//Inside onCreate() method.
LinearLayout firstLayout = (LinearLayout) findViewById(R.id.first_layout);
        Button button = new Button(this);
        button.setText(R.string.click_on_me);        
        firstLayout.addView(button);

enter image description here

问题:通过第2个案例可以明确,如果我想显式使用setWidth()方法,则不需要使用LayoutParams。那么在第4个案例中:将LinearLayout的宽度设置为“match_parent”,同时调用button.setWidth(20)。 但是为什么Button仍然不采用明确给定的宽度值,输出与CASE 1完全相同。

提前感谢。

2个回答

3

您需要为按钮视图定义适当的LayoutParams,然后将其添加到firstLayout中。

LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.height = XX;
params.width = XX;
button.setLayoutParams(params);

但是 setWidth() 的 API 说:“使 TextView 正好有这么多像素宽。您可以通过在 LayoutParams 中指定此数字来完成相同的操作。”我认为这意味着,我不一定必须使用 LayoutParams 方法,因为通过调用 setWidth(),我正在尝试定义相同的内容。 - itsMohitGoel

1
你需要理解为什么在动态创建按钮或其他组件时使用 LayoutParams 是必要的。
例如,如果您使用 LayoutParams 来给 Button 设置宽度。那么当我们设置 Button 的布局参数时,就是告诉父布局(在您的情况下是 LinearLayout)渲染 Button,将指定的高度和宽度设置为视图。因此,在渲染时可以正常工作。
在第一种情况下,它不影响设置宽度为20,因为默认情况下 Button 有最小宽度为 64dip。 在设置宽度之前将其设置为0。
btn.setMinimumWidth(0);

这个链接 这里 可以帮助你。


我不明白为什么需要LayoutParams。一个按钮应该有4个属性:左、上、宽度和高度。请解释一下为什么这些属性不足以满足需求?编程的一个通用原则是使用有意义的名称。因此,像“setWidth”这样的名称应该设置宽度。但在Android中,setWidth并没有设置宽度。为什么为什么为什么为什么…… - Paul McCarthy

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