如何动态地将按钮添加到视图以便布局宽度正常工作?

阿廖沙克

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

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

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

在 2 个(黄色)View 对象之间的 xml 中定义了 4 个切换按钮时显示的内容:

在此处输入图片说明

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

在此处输入图片说明

正确呈现的屏幕的 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 对象。

霍夫玛·德雷苏

您可能需要在调用 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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

动态地将视图添加到布局

来自分类Dev

如何动态地将 LinearLayoutCompat 视图添加到现有的 xml 布局?

来自分类Dev

动态地将JTextArea添加到Jpanel无法正常工作

来自分类Dev

动态地将视图添加到RecyclerView

来自分类Dev

如何动态地在滚动视图中将自定义布局添加到线性布局

来自分类Dev

如何动态地将Image添加到ImageView?

来自分类Dev

如何动态地将图像添加到imageView

来自分类Dev

动态地将xml布局添加到我当前的Activity布局中

来自分类Dev

如何动态地将图像从URL的字符串数组添加到活动的scrollview线性布局

来自分类Dev

动态将按钮添加到RelativeLayout不能正常工作

来自分类Dev

动态地将多个视图添加到ListView的行

来自分类Dev

动态地将超链接添加到gridview

来自分类Dev

动态地将数据添加到QTableView

来自分类Dev

动态地将标签添加到JTabbedPane

来自分类Dev

动态地将属性添加到类

来自分类Dev

动态地将内容添加到标签页

来自分类Dev

动态地将元素添加到HTML页面

来自分类Dev

如何动态地将QScrollArea添加到StackedWidget页面并向其中添加小部件?

来自分类Dev

如何动态地将类添加到焦点输入字段的父div?

来自分类Dev

如何动态地将新数组添加到现有数组中

来自分类Dev

如何动态地将行添加到特定的UITableView节?

来自分类Dev

如何动态地将ng-pattern属性添加到html元素?

来自分类Dev

如何动态地将外部定义的bean定义添加到Spring上下文?

来自分类Dev

如何动态地将子级添加到expandableListview的特定组中?

来自分类Dev

如何动态地将tinymce 4.x添加到textarea?

来自分类Dev

API调用后如何动态地将数据添加到网页?

来自分类Dev

如何动态地将一个类添加到Selectize选项中?

来自分类Dev

如何动态地将样式添加到React样式属性?

来自分类Dev

如何动态地将更多Kafka主题添加到Logstash配置

Related 相关文章

  1. 1

    动态地将视图添加到布局

  2. 2

    如何动态地将 LinearLayoutCompat 视图添加到现有的 xml 布局?

  3. 3

    动态地将JTextArea添加到Jpanel无法正常工作

  4. 4

    动态地将视图添加到RecyclerView

  5. 5

    如何动态地在滚动视图中将自定义布局添加到线性布局

  6. 6

    如何动态地将Image添加到ImageView?

  7. 7

    如何动态地将图像添加到imageView

  8. 8

    动态地将xml布局添加到我当前的Activity布局中

  9. 9

    如何动态地将图像从URL的字符串数组添加到活动的scrollview线性布局

  10. 10

    动态将按钮添加到RelativeLayout不能正常工作

  11. 11

    动态地将多个视图添加到ListView的行

  12. 12

    动态地将超链接添加到gridview

  13. 13

    动态地将数据添加到QTableView

  14. 14

    动态地将标签添加到JTabbedPane

  15. 15

    动态地将属性添加到类

  16. 16

    动态地将内容添加到标签页

  17. 17

    动态地将元素添加到HTML页面

  18. 18

    如何动态地将QScrollArea添加到StackedWidget页面并向其中添加小部件?

  19. 19

    如何动态地将类添加到焦点输入字段的父div?

  20. 20

    如何动态地将新数组添加到现有数组中

  21. 21

    如何动态地将行添加到特定的UITableView节?

  22. 22

    如何动态地将ng-pattern属性添加到html元素?

  23. 23

    如何动态地将外部定义的bean定义添加到Spring上下文?

  24. 24

    如何动态地将子级添加到expandableListview的特定组中?

  25. 25

    如何动态地将tinymce 4.x添加到textarea?

  26. 26

    API调用后如何动态地将数据添加到网页?

  27. 27

    如何动态地将一个类添加到Selectize选项中?

  28. 28

    如何动态地将样式添加到React样式属性?

  29. 29

    如何动态地将更多Kafka主题添加到Logstash配置

热门标签

归档