我有一个列表视图的这个 xml 布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/menu"
android:orientation="vertical" android:background="#2f4f4f" android:layout_width="wrap_content" android:layout_height="fill_parent">
<ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="#2f4f4f" android:cacheColorHint="#2f4f4f" android:scrollbars="none">
</ListView>
</LinearLayout>
线性布局默认占据全屏宽度。
我想以编程方式更改它。我想做这样的事情:
但是当我改变线性布局的宽度时没有任何反应,它仍然占据全屏宽度
LayoutInflater inflater = LayoutInflater.from(this);
View menu = inflater.inflate(R.layout.xml_layout, null);
menu.setLayoutParams(new LayoutParams(20,LayoutParams.WRAP_CONTENT));
在列表视图中硬编码布局宽度后:
<ListView
android:id="@+id/list"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:background="#2f4f4f"
android:cacheColorHint="#2f4f4f"
android:scrollbars="none" >
</ListView>
我明白了:
请提出一种方法来做到这一点。