如何在视图中动态添加按钮,以使布局宽度正确工作?

时间:2018-09-25 23:52:47

标签: android xml layout togglebutton programmatically-created

我试图向视图动态添加几个按钮,准确地说是ToggleButtons。如果我直接在xml中表示按钮,则显示时它们看起来正确。但是,当从xml扩展相同的按钮并使用addView(View,index)插入时,在绘制活动时我得到了不同的表示形式。

下面有两个屏幕截图。我正在尝试以编程方式重现您在第一个中看到的内容,但是正在获取第二个中的内容。请注意,已将ToggleButton对象添加到的LinearLayout已经具有两个View对象,它们均具有黄色背景色,并且在它们之间插入了切换按钮。 View对象通常是不可见的,并且是必需的,因此我可以将两端的间距定义为百分比,即0dp layout_width。

xml和代码也在下面。由于两种情况下我都使用相同的xml,为什么屏幕显示效果不相同?感谢您提供任何帮助,因为我必须以编程方式添加这些按钮,因为它们的编号在运行时未知(由服务器从json驱动)。

在两个(黄色)视图对象之间的xml中定义了四个切换按钮时显示的内容:

enter image description here

但是当将4个切换按钮以编程方式插入到两个(黄色)视图对象之间时:

enter image description here

正确呈现(根据需要)的屏幕的xml:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center"
    android:id="@+id/linLytRubCard"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="5dp"
        android:background="@color/ail_blue"
        >

        <ImageButton
            android:id="@+id/btnExpandDummy"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".10"
            android:padding="5dp"
            android:background="?android:attr/selectableItemBackground"
             />

        <TextView
            android:id="@+id/tvCardTitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".80"
            android:textColor="@color/black"
            android:textSize="16sp"
            android:gravity="center"
            android:layout_gravity="center_vertical"
            android:text="This-is-title"
            />

        <ImageButton
            android:id="@+id/btnExpandCollapse"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".10"
            android:padding="5dp"
            android:background="?android:attr/selectableItemBackground"
            android:src="@drawable/down_arrow_expand_rubric" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="5dp"
        android:id="@+id/linlytSegControl"
        >

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".10"
            android:background="@color/yellow"
            />

        <ToggleButton
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".20"
            android:gravity="center"
            android:paddingTop="8dp"
            android:paddingBottom="8dp"
            android:textOn="0"
            android:textOff="0"
            android:text="0"
            android:textSize="16dp"
            android:checked="false"
            android:background="@drawable/selector_leftmost_button_state"
            />

        <ToggleButton
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".20"
            android:gravity="center"
            android:paddingTop="8dp"
            android:paddingBottom="8dp"
            android:textOn="2"
            android:textOff="2"
            android:text="2"
            android:textSize="16dp"
            android:background="@drawable/selector_middle_button_state"
            android:checked="false"
            />

        <ToggleButton
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".20"
            android:gravity="center"
            android:paddingTop="8dp"
            android:paddingBottom="8dp"
            android:textOn="3"
            android:textOff="3"
            android:text="3"
            android:textSize="16dp"
            android:background="@drawable/selector_middle_button_state"
            android:checked="false"
            />

        <ToggleButton
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".20"
            android:gravity="center"
            android:paddingTop="8dp"
            android:paddingBottom="8dp"
            android:textOn="4"
            android:textOff="4"
            android:text="4"
            android:textSize="16dp"
            android:checked="false"
            android:background="@drawable/selector_rightmost_button_state"
            />

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".10"
            android:background="@color/yellow"
            />

    </LinearLayout>

我正在循环中创建和添加切换按钮,因此:

LinearLayout linLytSegCtrl = (LinearLayout) this.findViewById(R.id.linlytSegControl);

for (int x = 0; x < m_arrRubItemPointVals.size(); x++)
{
    ToggleButton tbn = (ToggleButton) View.inflate(m_context, R.layout.segmented_button_rubric, null);

    String sTitle = String.valueOf(x);
    tbn.setText(sTitle);
    tbn.setTextOn(sTitle);
    tbn.setTextOff(sTitle);
    linLytSegCtrl.addView(tbn, x+1); // inserts AFTER first yellow View in xml and before the second one
}

在运行时读取各个切换按钮的xml:

<ToggleButton
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight=".20"
    android:gravity="center"
    android:paddingTop="8dp"
    android:paddingBottom="8dp"
    android:textSize="16dp"
    android:background="@drawable/selector_middle_button_state"
    android:checked="false"
    />

是的,当我在运行时插入按钮时,我将它们从xml中删除,只剩下两个View对象。

1 个答案:

答案 0 :(得分:1)

您可能需要在调用中将父对象设置为view.inflate:View.inflate(m_context, R.layout.segmented_button_rubric, linLytSegCtrl);

View.Inflate的文档指出,第三个参数是“用于正确地增加layout_ *参数”:https://developer.android.com/reference/android/view/View.html#inflate(android.content.Context,%20int,%20android.view.ViewGroup)