如何在DrawerLayout中更改LinearLayout的参数?
我试过了:
LinearLayout linearLayout;
在onCreate之后:
drawerLinearLayout = (LinearLayout)findViewById(R.id.drawer);
// drawer is the id of the linearlayout, where I want to change its params
DrawerLayout.LayoutParams lp = new DrawerLayout.LayoutParams(100,100);
linearLayout.setLayoutParams(lp);
xml就像:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Main" />
<LinearLayout
android:id="@+id/drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
没有错误,但抽屉的尺寸不是100 x 100 - 它需要整个宽度和高度:S
编辑:
public class MainActivity extends FragmentActivity {
LinearLayout drawerLinearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
drawerLinearLayout = (LinearLayout)findViewById(R.id.drawer);
int mode = getResources().getConfiguration().orientation;
if (mode == 1) {
DrawerLayout.LayoutParams lp = new DrawerLayout.LayoutParams(2,2);
drawerLinearLayout.setLayoutParams(lp);
}
}}