2015-01-04 2 views
0

Здравствуйте, я хочу, чтобы макет выглядел как http://developer.android.com/images/training/layout-listitem.png вместо изображения, которое слева. Я хочу кнопку справа. Но моя кнопка сразу после того, как текстовое представление не попадает вправо. Я искал другие решения, но мне ничего не помогло. Вот что у меня есть.Правильное решение макета - андроид

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

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textColor="#000000" 
      android:textSize="12pt" 
      android:id="@+id/view_to" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textColor="#000000" 
      android:textSize="8pt" 
      android:id="@+id/view_what" /> 

    </LinearLayout> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_gravity="right" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 


     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="right" 
      android:text="@string/b_send" 
      android:id="@+id/b_send" /> 

    </LinearLayout> 

</LinearLayout> 

Заранее спасибо.

+2

Использование 'RelativeLayout'. Гораздо более гибкий для такого макета. – Simon

+0

моя кнопка не наклеивается на правый край, но сразу после длинного textView – miskohut

+0

в редакторе это нормально, но это происходит, когда я запускаю приложение. Я должен был упомянуть об этом. – miskohut

ответ

1

Использование RelativeLayout. Привяжите Button к правой стороне макета, используя android:layout_alignParentRight="true". Макет, который содержит TextViews, должен иметь android:layout_width="match_parent" и android:layout_toLeftOf="@id/<id-of-the-Button>", где должен быть заменен идентификатор Button.

+0

да я только что понял это :) android: layout_toLeftOf = "id" работал. – miskohut

0

Попробуйте это:

LinearLayout1 - Android: layout_weight = "3" (это займет около 75% пространства.),

LinearLayout2 - Android: layout_weight = "1" (это займет прибл. 25% пространства),

и для кнопки set layout_width и layout_height для «fill_parent» - это заставит кнопку занять все пространство в LinearLayout2.

Если вам нужно, чтобы кнопка занимала больше или меньше места по горизонтали, вы можете изменить layout_weight в LinearLayout1.

Это мой пример XML-файл:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_margin="5dp"> 


<LinearLayout 
    android:orientation="vertical" 
    android:id="@+id/LinearLayout1" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:layout_weight="3"> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="#999999" 
     android:layout_margin="5dp" 
     android:textSize="20sp" 
     android:layout_weight="1" 
     android:text="New Text" 
     android:id="@+id/textView" /> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="#777777" 
     android:layout_margin="5dp" 
     android:textSize="20sp" 
     android:layout_weight="1" 
     android:text="New Text" 
     android:id="@+id/textView2" /> 
</LinearLayout> 

<LinearLayout 
    android:orientation="vertical" 
    android:id="@+id/LinearLayout2" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_weight="1"> 

<Button 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#ff6633" 
    android:layout_margin="5dp" 
    android:layout_weight="1" 
    android:text="Button" 
    android:id="@+id/button" /> 
</LinearLayout> 

Это результат:

LinearLayout

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