Ich habe ein sehr einfaches Layout, das ich nicht so aussehen lassen kann, wie ich es möchte. Es ist ein LinearLayout mit einer Schaltfläche und einem Schalter. Ich möchte, dass sie übereinander angezeigt werden, aber ich möchte, dass ihre Breite der Hälfte des übergeordneten Layouts entspricht.
|--LinearLayout---------|
| |
| ----------- |
|| Switch | |
| ----------- |
| ----------- |
|| button | |
| ----------- |
------------------------
Ich habe mir andere ähnliche Antworten in SO angesehen, aber ich konnte keine Lösung finden, die für mich funktioniert. Das habe ich bisher versucht:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="2" >
<Switch
android:id="@+id/remember_me_switch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/remember" />
<Button
android:id="@+id/share_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="loginOnclick"
android:text="@string/login_button_text" />
</LinearLayout>
Damit nehmen der Knopf und der Schalter den gesamten Platz des Elternteils ein, anstatt nur die Hälfte. Ich habe es mit versucht android:layout_width = 0dp
bei den Kindern, aber es lässt sie verschwinden.
Irgendwelche Hilfe, bitte?
ja
Eine Möglichkeit besteht darin, ein horizontales Master-LinearLayout zu haben, das die Breite auf 2 aufteilt und darin das vertikale Layout enthält
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:orientation="vertical" >
<Switch
android:id="@+id/remember_me_switch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/remember" />
<Button
android:id="@+id/share_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="loginOnclick"
android:text="@string/login_button_text" />
</LinearLayout>
<!-- Right side spacer -->
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" />
</LinearLayout>
Der Trick ist zu verwenden 0dp für die Größe, die Sie verwalten möchten Layout_Gewicht ! Versuche dies
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:weightSum="2" >
<Switch
android:id="@+id/remember_me_switch"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:hint="@string/remember" />
<Button
android:id="@+id/share_button"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:onClick="loginOnclick"
android:text="@string/login_button_text" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1" >
</LinearLayout>
</LinearLayout>
Kurze Antwort:
Verwenden android:weightSum=”2″ in Ihrem horizontalen übergeordneten Layout
Verwenden android:layout_weight=”1″ und android:layout_width=”0dp” in Ihren Kinderlayouts.
Versuchen Sie diesen Code
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="2" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Switch
android:id="@+id/remember_me_switch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/remember" />
<View
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/share_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="loginOnclick"
android:text="@string/login_button_text" />
<View
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
Kalai.G
Button button = (Button) findViewById(R.id.share_button);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = displaymetrics.widthPixels;
int buttonWidth = width/2;
setze dies buttonWidth
zu Ihrem Button wie button.setWidth(buttonWidth);
Giulio Caccin
int params = linearLayout.getWidth()/2;
ViewGroup.LayoutParams( params, ViewGroup.LayoutParams.WRAP_CONTENT));
105447 0 0 cookie-check Android-Layout: Breite der Hälfte des übergeordneten Elements yes
layout_weight
wird nur auf das mit angegebene Maß angewendetorientation
. Das heißt, die Gewichtung bezieht sich auf die Höhe in vertikaler und die Breite in horizontalen linearen Layouts.– laalto
15. November 2013 um 12:08 Uhr