我正在尝试向视图动态添加几个按钮,准确地说是 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] 删除。
我来说两句