W moim Kodzie mam plik XML z wieloma układami podrzędnymi. Ai po prostu chcę ustawić programowo jedną z tych rozkładów podrzędnych według szerokości ekranu urządzenia. do tej pory wypróbowałem parametry Layout, setparam i inne metody, ale pokazuje wyjątek wskaźnika pustego na uzyskanie identyfikatora układu.Ustaw programowo szerokość układu dziecka w ANDROID?
mój układ rodzic jest względne rozmieszczenie, i 1 Układ Dziecko jest układ liniowy ("w tym układzie muszę zmienić szerokość")
<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>
kod 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;
}
Oto fragment mojego kodu i komentarz do części, którą próbowałem do tej pory, i uzyskałem dobry wynik .. ``
Gdzie jest kod? –
Nie publikuj swojego pliku XML jako zrzutu ekranu, podaj właściwy kod w pytaniu. – Cerbrus
Odpowiedź została udzielona tutaj http://stackoverflow.com/questions/5715612/how-to-set-setlayoutparams-for-linear-layout-elements –