在我的代碼中,我有一個XML文件有多個子佈局。 Aand我只想根據設備的屏幕寬度以編程方式設置其中一個子佈局的寬度。 直到現在我已經嘗試了佈局參數,setparam和其他方法,但它顯示空指針異常獲取佈局ID。以編程方式設置子版面寬度在ANDROID中?
我的父母佈局是一個相對佈局, 和第一個孩子佈局是一種線性佈局(「此佈局我需要改變寬度」)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parentlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/childlayout"
android:layout_width="250dip"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#153746">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/sub_title"
android:gravity="center_vertical" >
<TextView
android:id="@+id/menu_header_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="asd"
android:textColor="#ffffff"
android:layout_marginLeft="10dip" />
<ImageView
android:id="@+id/menu_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
</LinearLayout>
<ListView
android:id="@+id/menu_listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@android:color/darker_gray"
android:dividerHeight="1dip"
android:scrollingCache="false" />
</LinearLayout>
<FrameLayout
android:id="@+id/overlay"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
</LinearLayout>
Java代碼: -
public class SlideMenu extends LinearLayout {
// keys for saving/restoring instance state
public final static String KEY_MENUSHOWN = "menuWasShown";
private final static String KEY_STATUSBARHEIGHT = "statusBarHeight";
private final static String KEY_SUPERSTATE = "superState";
// Display display = ((WindowManager) getWindowToken()).getDefaultDisplay();
// int width = display.getWidth(); // deprecated
// int height = display.getHeight(); // deprecated
// float layWidth = (width*70)/100;
//
//// LinearLayout view= (LinearLayout)findViewById(R.id.childlayout);
//// LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
//// params.width = 130;
//// view.setLayoutParams(params);
// view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, theSizeIWant));
public static class SlideMenuItem {
public int id;
public Drawable icon;
public String label;
}
這裏是我的代碼片段,我評論我到目前爲止所嘗試的部分,並得到任何好的結果..''
哪裏是你的代碼? –
不要將您的XML作爲屏幕截圖發佈,請將實際代碼放在問題中。 – Cerbrus
這是在這裏回答http://stackoverflow.com/questions/5715612/how-to-set-setlayoutparams-for-linear-layout-elements –