내 코드에는 여러 자식 레이아웃이있는 XML 파일이 있습니다. Aand 그냥 장치의 화면 너비에 따라 이러한 자식 레이아웃의 너비 중 하나를 프로그래밍 방식으로 설정하려고합니다. 지금까지 레이아웃 매개 변수, setparam 및 기타 메서드를 시도했지만 레이아웃 ID를 가져 오는 데 null 포인터 예외가 표시됩니다.자식 레이아웃 너비를 프로그래밍 방식으로 설정 ANDROID?
내 부모 레이아웃은 상대 레이아웃, 및 제 1 회 아이의 레이아웃은 선형 레이아웃 ("이 레이아웃에 내가 폭 변경해야")
<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>
자바 코드입니다 : -
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 –