2012-03-23 3 views
9

Итак, у меня есть этот макет:андроид «layout_alignParentBottom» в относительном макете

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/layout" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:background="#FFFF00" 
       android:minHeight="100dp" 
       android:layout_gravity="bottom" 
     > 
    <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:textColor="#000000" 
      android:background="#FF0000" 
      android:text="Hello World" 
      /> 

    <Button 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:layout_alignParentRight="true" 
      android:text="button"/> 
</RelativeLayout> 

и вот как это выглядит:

enter image description here

, но если добавить android:layout_alignParentBottom="true" к кнопке здесь как это выглядит:

enter image description here

  1. Может ли кто-нибудь объяснить мне такое поведение?
  2. Как поместить мою кнопку внизу, не изменяя размер желтого макета и не добавляя тысячи макетов для обходных решений?
+0

Остается ли эта ошибка? –

+0

Я столкнулся с этой проблемой в Android 2.2 и 4.0.0, но я думаю, что это случилось на других платформах тоже –

ответ

9

это решение работало для меня

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/layout" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:background="#FFFF00" 
       android:minHeight="100dp" 
       android:orientation="horizontal" 
       android:layout_gravity="bottom" 
     > 
    <TextView 
      android:id="@+id/tv" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:textColor="#000000" 
      android:background="#FF0000" 
      android:text="Hello World" 
      /> 

    <Button 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:layout_gravity="bottom|right" 
      android:text="button"/> 
</FrameLayout> 
0
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/layout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_gravity="bottom" 
    android:background="#FFFF00" 
    android:minHeight="100dp" > 

    <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:textColor="#000000" 
      android:background="#FF0000" 
      android:text="Hello World"/> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:text="button" /> 

</RelativeLayout> 

попробуйте этот код

+0

, используя ваш код, чтобы мой макет охватывал весь экран, потому что вы использовали андроид: layout_height = "fill_parent" и я хотите, чтобы мой макет сохранил его высоту, как видно на моем первом снимке экрана –

+0

, в котором вы можете использовать один код? – Android

+0

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

1

это один быстрый один экран

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/layout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_gravity="bottom" 
    android:background="#FFFF00" 
    android:minHeight="100dp" > 



    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:text="button" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignTop="@+id/button1" 
     android:background="#000000" 
     android:text="TextView" /> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/button1" 
     android:layout_alignParentLeft="true" 
     android:background="#000000" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:orientation="vertical" > 

    </LinearLayout> 

</RelativeLayout> 
+0

это не работает, потому что не учитывается minHeight от основного относительного макета –

1

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

0

К сожалению, ошибка все еще существует в более новых версиях Android.

Во всяком случае, я обнаружил, что после может решить эту проблему:

  1. Удалить MinHeight из RelativeLayout

  2. Вставьте TextView в LinearLayout:

    <LinearLayout 
        android:id="@+id/tvLayout" 
        android:layout_alignParentTop="true" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:orientation="vertical" 
        android:minHeight="100dp" 
        android:gravity="top" 
        > 
        <TextView 
         android:id="@+id/tv" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:background="#FFFF00FF" 
         android:text="Helo" 
         /> 
    </LinearLayout> 
    
  3. Удалить layout_alignParentBottom от кнопки и add layout_alignBottom = "@ id/tvLayout"

Теперь LinearLayout «контролирует» высоту RelativeLayout. Если высота TextView больше, чем minHeight 100dp, она будет расширяться.

И кнопка всегда будет выровнять ее нижнюю к LinearLayout, равную RelativeLayout.

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