2016-11-04 3 views
1

У меня есть linearlayout, который имеет семь кнопок внутри. Однако кнопки не отображают свой текст, потому что они помещены неправильно. Вот почему я установил weightsum на 7 и weight на 1 для каждой кнопки. Отчасти это сработало, так как я получил ожидаемый результат на виртуальном устройстве. Тем не менее, когда я установил приложение на свой телефон, кнопки все еще прятались. Что мне делать?Поместите семь кнопок рядом друг с другом в linearlayout

LinearLayout XML:

<LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="50dp" 
      android:weightSum="7" 
      android:id="@+id/linearLayout" 
android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" android:baselineAligned="false"> 
     <Button style="?android:attr/buttonStyleSmall" android:layout_width="0dp" android:layout_height="wrap_content" 
       android:text="MON" android:id="@+id/mon" android:checked="false" 
       android:singleLine="true" android:layout_weight="1"/> 
     <Button style="?android:attr/buttonStyleSmall" android:layout_width="0dp" android:layout_height="wrap_content" 
       android:text="TUE" android:id="@+id/tue" android:checked="false" 
       android:singleLine="true" android:layout_weight="1"/> 
     <Button style="?android:attr/buttonStyleSmall" android:layout_width="0dp" android:layout_height="wrap_content" 
       android:text="WED" android:id="@+id/wed" android:checked="false" 
       android:singleLine="true" android:layout_weight="1"/> 
     <Button style="?android:attr/buttonStyleSmall" android:layout_width="0dp" android:layout_height="wrap_content" 
       android:text="THU" android:id="@+id/thu" android:checked="false" 
       android:singleLine="true" android:layout_weight="1"/> 
     <Button style="?android:attr/buttonStyleSmall" android:layout_width="0dp" 
       android:layout_height="wrap_content" android:text="FRI" android:id="@+id/fri" 
       android:checked="false" android:singleLine="true" android:layout_weight="1"/> 
     <Button style="?android:attr/buttonStyleSmall" android:layout_width="0dp" 
       android:layout_height="wrap_content" android:text="SAT" android:id="@+id/sat" 
       android:checked="false" android:singleLine="true" android:layout_weight="1"/> 
     <Button style="?android:attr/buttonStyleSmall" android:layout_width="0dp" 
       android:layout_height="wrap_content" android:text="SUN" android:id="@+id/sun" 
       android:checked="false" android:singleLine="true" android:layout_weight="1"/> 
    </LinearLayout> 

разрешение виртуального устройства (если бы я получить ожидаемый результат): 768x1280

разрешение реального устройства (в котором у меня есть проблемы): 480x854

Скриншот :This is a screenshot of the result from the final accepted answer

+0

вставьте свой код xml –

+0

Опубликуйте свой xml-макет и, если возможно, разрешение телефона, на котором вы его попробовали. – Oasa

+0

Скриншоты также могут помочь. – CaptainBli

ответ

1

Пожалуйста, попробуйте этот код .. Надеюсь, что это работает

<LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:weightSum="7"> 
      <Button 
       android:id="@+id/bt1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="MON" 
       android:weight="1"/> 

      <Button 
       android:id="@+id/bt2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="TUE" 
       android:weight="1"/> 
      <Button 
       android:id="@+id/bt3" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="WED" 
       android:weight="1"/> 
      <Button 
       android:id="@+id/bt4" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="THU" 
       android:weight="1"/> 
      <Button 
       android:id="@+id/bt5" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="FRI" 
       android:weight="1"/> 
      <Button 
       android:id="@+id/bt6" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="SAT" 
       android:weight="1"/> 
      <Button 
       android:id="@+id/bt7" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="SUN" 
       android:weight="1"/> 

    </LinearLayout> 

Обновлено Позвольте мне знать, если он работает или нет

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

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

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"> 

      <Button 
       android:id="@+id/bt1" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="MON" 
       android:weight="1"/> 

     </LinearLayout> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"> 

      <Button 
       android:id="@+id/bt2" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="TUE" 
       android:weight="1"/> 

     </LinearLayout> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"> 

      <Button 
       android:id="@+id/bt3" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="WED" 
       android:weight="1"/> 

     </LinearLayout> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"> 

      <Button 
       android:id="@+id/bt4" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="THU" 
       android:weight="1"/> 

     </LinearLayout> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"> 

      <Button 
       android:id="@+id/bt5" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="FRI" 
       android:weight="1"/> 

     </LinearLayout> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"> 

      <Button 
       android:id="@+id/bt6" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="SAT" 
       android:weight="1"/> 

     </LinearLayout> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"> 

      <Button 
       android:id="@+id/bt7" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="SUN" 
       android:weight="1"/> 

     </LinearLayout> 

    </LinearLayout> 

</LinearLayout> 
+0

Как вы можете видеть, он пытается использовать layout_weight, чтобы он был отзывчивым. Создание ширины как 'wrap_content', безусловно, отобразит кнопку как полную, но не так, как автор хочет. Также я считаю, что большинство кнопок обычно не видны для устройств с низким разрешением. – Oasa

+0

@ vishnumm93 это не сработало. Позиционирование кнопок в порядке, но их текст не отображается. –

+0

@David_David Я обновил код .. – vishnumm93

0

Вы можете попробовать использовать TableLayout в LinearLay вне. Таблица будет иметь один ряд, но семь столбцов. Таким образом, у вас будут кнопки, равномерно распределенные.

<TableLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:weightSum="7"> 

     <TableRow 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 
      <Button 
       android:id="@+id/bt1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/bt1"/> 

      <Button 
       android:id="@+id/bt2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/bt2"/> 
      <Button 
       android:id="@+id/bt3" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/bt3"/> 
      <Button 
       android:id="@+id/bt4" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/bt4"/> 
      <Button 
       android:id="@+id/bt5" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/bt5"/> 
      <Button 
       android:id="@+id/bt6" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/bt6"/> 
      <Button 
       android:id="@+id/bt7" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/bt7"/> 

     </TableRow> 
    </TableLayout> 

Вам также нужно будет добавить поля для каждой кнопки, чтобы указать, как они отделены друг от друга.

+0

Я сомневаюсь, что это сработает с тех пор width устанавливается на 'wrap_content' на каждом представлении. – Oasa

+0

@ TheLearner К сожалению, это не сработало. –

0

как разрешение экрана уменьшается, кнопка косяк подгоните текст по нормальному размеру. Поскольку ширина задается параметром layout_weight, он не может отображать текст в ограниченном пространстве. Вам нужно уменьшить размер текста, если вы хотите увидеть текст. Если вам нужно автомасштабировать размер текста, посмотрите на это link.

С другой стороны, вы можете сделать это двумя линиями кнопок для устройств с более низким разрешением. Или найдите альтернативы кнопке в зависимости от того, чего вы пытаетесь достичь.

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