Поместите 3 кнопки в LinearLayout, чтобы занять равное количество места
Я хотел бы иметь три кнопки, занимая равное количество доступного пространства по горизонтали подряд.
Я использовал android:layout_gravity
. В чем проблема?
layout xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1.0"
>
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Bg"
android:background="@drawable/button_red"
android:layout_weight=".2"
android:layout_gravity="left"
/>
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Bm"
android:background="@drawable/button_red"
android:layout_weight=".2"
android:textColor="#ffffff"
android:layout_gravity="center"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_red"
android:layout_weight=".2"
android:text="@string/Bf"
android:layout_gravity="right"
/>
</LinearLayout>
Если кто-то видит, что не так, спасибо.
Ответы
Ответ 1
Следующий макет должен работать. весы для LinearLayouts..
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3"
>
<Button android:id="@+id/button1"
...
android:layout_weight="1"/>
<Button android:id="@+id/button2"
...
android:layout_weight="1"/>
<Button
android:id="@+id/button3"
...
android:layout_weight="1"/>
</LinearLayout>
Ответ 2
помимо установки a layout_weight
вам нужно установить layout_width
или layout_height
на 0dp
. Поэтому, если вы хотите, например, распределить кнопки по горизонтали, layout_width
должно быть 0dp и layout_weight
.2 или любое число, если оно равно с помощью кнопок, которые у вас есть.
поэтому ваш макет должен быть таким
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/Bg"
android:background="@drawable/button_red"
android:layout_weight="1"
/>
<Button android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/Bm"
android:background="@drawable/button_red"
android:layout_weight="1"
android:textColor="#ffffff"
/>
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@drawable/button_red"
android:layout_weight="1"
android:text="@string/Bf"
/>
</LinearLayout>
Ответ 3
Разделите сумму веса на равную долю во всех кнопках.
Удалить свойство layout_gravity и добавить android: layout_weight = 0.33.
Он будет работать
Ответ 4
Проверьте это:
<RelativeLayout
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/button1"/>
</RelativeLayout>
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/>
</RelativeLayout>
Ответ 5
Поместите кнопки в относительный макет. добавьте свойства к кнопкам allignParentleft = true, еще один allignParentcenter = true и allignParentRight = true для каждой кнопки.
Ответ 6
Пожалуйста, рассмотрите следующий фрагмент раскладки:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="3">
<ImageView
android:src="@drawable/logo1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="left|center_vertical" />
<TextView
android:id="@+id/toolbar_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Some Title"
android:textColor="@android:color/black"
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="1" />
<ImageView
android:src="@drawable/logo2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="right|center_vertical" />
</LinearLayout>
2 вещи, которые следует отметить выше.
а. Я создал LinearLayout с weightSum из 3.
В. Затем внутри я создаю 3 элемента, каждый из которых имеет layout_weight из 1, так что я могу иметь, чтобы мои дочерние элементы равномерно распределяли все пространство между собой.
Ответ 7
Я делаю это прагматично без веса
float width = CommonUtills.getScreenWidth(activity);
int cardWidth = (int) CommonUtills.convertDpToPixel(((width) / 3), activity);
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(cardWidth,
LinearLayout.LayoutParams.MATCH_PARENT);
btnOne.setLayoutParams(params);
btnTwo.setLayoutParams(params);
btnThree.setLayoutParams(params);
public class CommonUtills {
public static float getScreenWidth(Context context) {
float width = (float) 360.0;
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
width = displayMetrics.widthPixels / displayMetrics.density;
return width;
}
}
<LinearLayout
android: layout_width = "match_parent"
android: layout_height = "50dp"
android: orientation = "horizontal" >
<Button
android: id = "@+id/btnOne"
android: layout_width = "wrap_content"
android: layout_height = "match_parent" > </Button>
<Button
android: id = "@+id/btnTwo"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content" > </Button>
<Button
android: id = "@+id/btnThree"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content" > </Button>
</LinearLayout>
Ответ 8
Вы можете разделить его так:
`
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/imageButtonLikedPost">
<ImageButton
android:id="@+id/imageButtonMyPost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/mypost_tab_bg" />
</RelativeLayout>
<ImageButton
android:id="@+id/imageButtonLikedPost"
android:layout_width="40dp"
android:layout_height="30dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/mylike_tab_bg" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/imageButtonLikedPost">
<ImageButton
android:id="@+id/imageButtonMyBlog"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/tab4_bg" />
</RelativeLayout>
</RelativeLayout>`