我创建了一个简单的应用程序来说明在包含在SDK 17和SDK 18之间的RelativeLayout中时LinearLayout的行为方式的变化.首先,截图:
当targetSdkVersion为“17”时:
当targetSdkVersion为“18”时:
此活动的布局是:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ffff"
android:orientation="vertical" >
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:overScrollMode="never"
android:scrollingCache="false" >
text_list_item.xml是:
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff0000" >
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#ffff00"
android:padding="10dp"
android:text="foobar"
android:textColor="#000000" />
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="@id/text"
android:layout_alignTop="@id/text"
android:background="#00ff00"
android:orientation="horizontal"
android:weightSum="1.0" >
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:background="#0000ff" />
活动的onCreate()如下所示:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
findViewById(R.id.text).bringToFront();
final ListView mListView = (ListView) findViewById(R.id.listview);
mListView.setAdapter(new TestAdapter());
}
而TestAdapter看起来像这样:
public class TestAdapter extends BaseAdapter {
private static final int COUNT = 3;
@Override
public int getCount() {
return COUNT;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public boolean areAllItemsEnabled() {
return true;
}
@Override
public boolean isEnabled(int position) {
return false;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.test_list_item, null);
view.findViewById(R.id.text).bringToFront();
return view;
}
当target = 17时,LinearLayout的子视图在ListView内部和ListView外部时,可以正确调整为可用宽度的50%.当target = 18时,当用作列表项的布局时,它不会调整大小.在这种情况下,它的宽度保持在“0dp”并且永远不会调整大小.
理想情况下,我想针对SDK 19.但是,为了做到这一点,我要以某种方式适应这种行为的变化.有关如何实现这一点的任何想法?
顺便说一句,有些原因导致布局如此复杂和“怪异”;我意识到有更直接的方法来实现这种特殊的视觉效果.
编辑:
为了更多地了解我为什么使用这种病态布局:它用于“仪表”.使用ScaleAnimation拉伸或缩小LinearLayout内的视图,使其占据总可用宽度的所需百分比.让它变得棘手的是我需要在仪表顶部覆盖文本,我希望仪表的高度由文本决定.
因此,外部RelativeLayout应该“与其父级一样宽”,但只“与它包含的TextView一样高”. RelativeLayout中的LinearLayout应该与其父级一样宽和高. LinearLayout中的View应该与包含它的LinearLayout一样高,但只有一半宽.
当布局未用作ListView项目的内容时,这正是我在针对SDK 17和/或SDK 18时获得的行为.
编辑
发布在开发人员组: