-1

Я пытаюсь центрировать кнопку в середине других двух кнопок. Я пробовал использовать weight, но не повезло (Android Studio продолжайте говорить, что есть ошибка), используя layout_marginRight - это не очень хорошая идея, потому что это не разрешение независимо. Вот мой код XML Надеюсь, он поможет вам понять мою проблему.Центральная кнопка между двумя другими кнопками

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:showIn="@layout/app_bar_main" 
tools:context=".MainActivity"> 

<TextView 
    android:text="Hello World!" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar_bottom" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:background="?attr/colorPrimary" 
    android:layout_alignParentBottom="true" 
    android:minHeight="?attr/actionBarSize" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal" 
     android:gravity="center"> 

     <RelativeLayout 
      android:id="@+id/relativeLayout1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" > 

      <Button 
       android:id="@+id/bottom_LeftButton" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Lft" 
       android:layout_alignParentLeft="true" /> 

      <Button 
       android:id="@+id/bottom_LeftCenterButton" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="LftC" 
       android:layout_alignParentLeft="true" /> 

      <Button 
       android:id="@+id/bottom_CenterButtom" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerInParent="true" 
       android:text="Cnt" /> 

      <Button 
       android:id="@+id/bottom_RightCenterButton" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="RhtC" 
       android:layout_alignParentRight="true" /> 

      <Button 
       android:id="@+id/bottom_RightButton" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Rht" 
       android:layout_alignParentRight="true" /> 

     </RelativeLayout> 
    </LinearLayout> 
</android.support.v7.widget.Toolbar> 

+0

Что об использовании LinearLayout с весом? – UpmostScarab

+1

использовать 'linearLayout'. –

+0

Вы не можете использовать весы в RelativeLAyouts. –

ответ

2
<LinearLayout 
     android:id="@+id/relativeLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

     <Button 
      android:id="@+id/bottom_LeftButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Lft" 
      android:layout_weight="1" 
      android:layout_alignParentLeft="true" /> 

     <Button 
      android:id="@+id/bottom_LeftCenterButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="LftC" 
      android:layout_weight="1" 
      android:layout_alignParentLeft="true" /> 

     <Button 
      android:id="@+id/bottom_CenterButtom" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:text="Cnt" /> 

     <Button 
      android:id="@+id/bottom_RightCenterButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="RhtC" 
      android:layout_weight="1" 
      android:layout_alignParentRight="true" /> 

     <Button 
      android:id="@+id/bottom_RightButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Rht" 
      android:layout_weight="1" 
      android:layout_alignParentRight="true" /> 

    </LinearLayout> 
+0

Это работает как шарм, который я не знал об RelativeLayout и Weight. У каждой кнопки теперь разный размер, как я могу получить пять одинаковых размеров? – user3320009

+0

Удалить андроид: layout_alignParentRight = "true" и установить вес как 3 во всех кнопках @ # user3320009 – YUVRAJ

+0

Работал! Огромное спасибо. – user3320009

0

Использование линейной компоновки, то вы включаете «вес» в вашей кнопки, так как вес принадлежит LinearLayout.

2

Чтобы сохранить все кнопки одинакового размера, вам необходимо сохранить атрибут width в «fill_parent». Ниже приведен рабочий XML-файл. weightsum должен быть равен числу кнопок, которые вы хотите использовать.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

<LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:weightSum="4"> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Button" /> 
    <Button 
     android:id="@+id/button2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Button" /> 
    <Button 
     android:id="@+id/button3" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Button" /> 
    <Button 
     android:id="@+id/button4" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Button" /> 


    </LinearLayout> 

+0

для трех кнопок сохранить весов как 3 –

+0

'weightSum' на самом деле ** бесполезно **. и 'fillParent' ** устарел ** с API уровня 8. –

+0

ну, вы можете заменить fill_parent match_parent @ FrankN.Stein. Это решение сохраняет ширину элементов одинаково, даже если длина текста увеличивается –

Смежные вопросы