代码之家  ›  专栏  ›  技术社区  ›  Alyoshak

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

  •  0
  • Alyoshak  · 技术社区  · 6 年前

    我正在尝试动态地向视图添加几个按钮,精确地切换按钮。如果我在XML中直接表示按钮,那么它们在显示时看起来是正确的。但是,当相同的按钮从XML膨胀并用addview(视图、索引)插入时,在绘制活动时,我会得到不同的表示。

    下面有两个截图。我正在尝试以编程的方式复制您在第一个示例中看到的内容,但正在获取第二个示例。请注意,要添加到的ToggleButton对象的LinearLayout已经有两个视图对象,它们都具有黄色背景色,并且正在它们之间插入切换按钮。视图对象通常是不可见的,并且是必需的,因此我可以将末端的间距定义为百分比,因此0dp布局宽度。

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

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

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

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

    <linearlayout
    android:layout_width=“匹配父级”
    android:layout_height=“包装内容”
    android:orientation=“垂直”
    android:gravity=“中心”
    安卓:id=“@+id/LinlyTrubCard”
    gt;
    
    <线性布局
    android:layout_width=“匹配父级”
    android:layout_height=“包装内容”
    android:orientation=“水平”
    安卓:padding=“5dp”
    android:background=“@color/ail_blue”
    gt;
    
    图像按钮
    android:id=“@+id/btnexpanddummy”
    android:layout_width=“0dp”
    android:layout_height=“包装内容”
    android:布局“权重=”10“
    安卓:padding=“5dp”
    android:background=“?android:attr/selectableitemsbackground“
    /gt;
    
    文本视图
    安卓:id=“@+id/tvcarditle”
    android:layout_width=“0dp”
    android:layout_height=“包装内容”
    android:布局“权重=”80“
    android:textcolor=“@color/black”
    android:textsize=“16sp”
    android:gravity=“中心”
    android:layout_gravity=“center_vertical”
    android:text=“这是标题”
    /gt;
    
    图像按钮
    android:id=“@+id/btnExpandCollapse”
    android:layout_width=“0dp”
    android:layout_height=“包装内容”
    android:布局“权重=”10“
    安卓:padding=“5dp”
    android:background=“?android:attr/selectableitemsbackground“
    android:src=“@drawable/down_arrow_expand_rubric”/>gt;
    
    </linearlayout>
    
    <线性布局
    android:layout_width=“匹配父级”
    android:layout_height=“包装内容”
    android:orientation=“水平”
    安卓:padding=“5dp”
    android:id=“@+id/linlytsegcontrol”(Android:id=“@+id/linlytsegcontrol”)。
    gt;
    
    视野;视野
    android:layout_width=“0dp”
    android:layout_height=“匹配父级”
    android:布局“权重=”10“
    android:background=“@color/yellow”
    /gt;
    
    <切换按钮
    xmlns:android=“http://schemas.android.com/apk/res/android”
    android:layout_width=“0dp”
    android:layout_height=“包装内容”
    android:布局“权重=”20“
    android:gravity=“中心”
    安卓:paddingtop=“8dp”
    安卓:paddingbottom=“8dp”
    安卓:texton=“0”
    安卓:textoff=“0”
    安卓:text=“0”
    android:textsize=“16dp”
    android:checked=“假”
    android:background=“@drawable/selector_leftmost_button_state”
    /gt;
    
    <切换按钮
    xmlns:android=“http://schemas.android.com/apk/res/android”
    android:layout_width=“0dp”
    android:layout_height=“包装内容”
    android:布局“权重=”20“
    android:gravity=“中心”
    安卓:paddingtop=“8dp”
    安卓:paddingbottom=“8dp”
    安卓:texton=“2”
    安卓:textoff=“2”
    安卓:text=“2”
    android:textsize=“16dp”
    android:background=“@drawable/selector_middle_button_state”
    android:checked=“假”
    /gt;
    
    <切换按钮
    xmlns:android=“http://schemas.android.com/apk/res/android”
    android:layout_width=“0dp”
    android:layout_height=“包装内容”
    android:布局“权重=”20“
    android:gravity=“中心”
    安卓:paddingtop=“8dp”
    安卓:paddingbottom=“8dp”
    安卓:texton=“3”
    安卓:textoff=“3”
    安卓:text=“3”
    android:textsize=“16dp”
    android:background=“@drawable/selector_middle_button_state”
    android:checked=“假”
    /gt;
    
    <切换按钮
    xmlns:android=“http://schemas.android.com/apk/res/android”
    android:layout_width=“0dp”
    android:layout_height=“包装内容”
    android:布局“权重=”20“
    android:gravity=“中心”
    安卓:paddingtop=“8dp”
    安卓:paddingbottom=“8dp”
    安卓:texton=“4”
    安卓:textoff=“4”
    安卓:text=“4”
    android:textsize=“16dp”
    android:checked=“假”
    android:background=“@drawable/selector_rightmost_button_state”
    /gt;
    
    视野;视野
    android:layout_width=“0dp”
    android:layout_height=“匹配父级”
    android:布局“权重=”10“
    android:background=“@color/yellow”
    /gt;
    
    </linearlayout>
    < /代码> 
    
    

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

    linearlayout linlytsegctrl=(linearlayout)this.findViewByID(r.id.linlytsegcontrol);
    
    对于(int x=0;x<m_arrrubitempointvals.size();x++)
    {
    toggleButton tbn=(toggleButton)view.inflat(m_context,r.layout.segmented_button_rubric,空);
    
    string stile=string.valueof(x);
    tbn.settext(缝线);
    tbn.settexton(针脚);
    tbn.settextoff(缝线);
    linlytsegctrl.addview(tbn,x+1);//在XML中第一个黄色视图之后和第二个黄色视图之前插入
    }
    < /代码> 
    
    

    运行时读取的各个切换按钮的XML:。

    <toggleButton
    xmlns:android=“http://schemas.android.com/apk/res/android”
    android:layout_width=“0dp”
    android:layout_height=“包装内容”
    android:布局“权重=”20“
    android:gravity=“中心”
    安卓:paddingtop=“8dp”
    安卓:paddingbottom=“8dp”
    android:textsize=“16dp”
    android:background=“@drawable/selector_middle_button_state”
    android:checked=“假”
    /gt;
    < /代码> 
    
    

    是的,当我在运行时插入按钮时,我会将它们从XML中删除,只留下两个视图对象。

    下面有两个截图。我正在尝试以编程的方式复制您在第一个示例中看到的内容,但正在获取第二个示例。请注意,要添加到的ToggleButton对象的LinearLayout已经有两个视图对象,它们都具有黄色背景色,并且正在它们之间插入切换按钮。视图对象通常是不可见的,并且是必需的,因此我可以将端点上的间距定义为百分比,从而定义0dp布局宽度。

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

    当4个切换按钮在2个(黄色)视图对象之间的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中删除,只留下两个视图对象。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Hofma Dresu    6 年前

    您可能需要在调用中设置父级以查看。充气: View.inflate(m_context, R.layout.segmented_button_rubric, linLytSegCtrl); .

    view.inflat的文档说明第三个参数“用于正确充气布局参数”: https://developer.android.com/reference/android/view/View.html#inflate(android.content.Context,%20int,%20android.view.ViewGroup)