2013-12-17 2 views
6

У меня есть linearlayoutКак выровнять родительское дно в линейной компоновке Android?

и я хочу создать в нижней части его фрагмент.

Я знаю, что есть несколько вариантов, но я немного запутался

1) android:layout_gravity:"bottom" -> это не работает для меня по какой-то причине.

2) android:gravity_weight="0" и дать родственный, прежде чем он android:gravity_weight:"1"

3) android:height="wrap_content" и дать родственный, прежде чем он android:height:"match_parent"

Я знаю, как это сделать с помощью RelativeLayout, но я хочу, чтобы практиковать LinearLayout

что бы вы предложили?

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

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="10dp" 
     android:layout_marginTop="5dp" 
     android:src="@drawable/signup_illu_why" /> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="" 
     android:orientation="horizontal" /> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:src="@drawable/signup_skip_icon" /> 

</LinearLayout> 
+3

Вы пробовали использовать RelativeLayout? Это может быть лучше подходит для ваших нужд, и RelativeLayout более эффективен и рекомендуется в целом. – janos

+0

Я знаю, как это сделать, используя relativeLayout, но я хочу практиковать linearLayout –

+0

Аналогичный - проверить решение http://stackoverflow.com/a/31987803/3624307 – ashumeow

ответ

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:layout_weight="0.8" > 

     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:src="@drawable/ic_launcher" /> 

     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:src="@drawable/ic_launcher" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_weight="0.2" /> 

</LinearLayout> 
+0

Вы не можете добавить 'android: orientation' в RelativeLayout. –

+0

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

+0

Я знаю, как это сделать, используя relativeLayout, но я хочу практиковать linearLayout –

1

попробовать этот

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

     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 

      android:src="@drawable/ic_launcher" /> 
     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:orientation="vertical" > 

     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:src="@drawable/ic_launcher" /> 

    </LinearLayout> 

    </RelativeLayout> 
14

На самом деле, вы можете установить гравитацию родительского элемента в нижний

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

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="10dp" 
     android:layout_marginTop="5dp" 
     android:src="@drawable/signup_illu_why" /> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="" 
     android:orientation="horizontal" /> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:src="@drawable/signup_skip_icon" /> 

</LinearLayout> 
+0

, но тогда все будет внизу – user25

+0

это правильный ответ - http://stackoverflow.com/a/20633003/4548520 мы должны использовать 'RelativeLayout' как родительский и установить' android: layout_alignParentBottom = "true" 'для ребенка, что мы хотим быть внизу – user25

+0

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

0

окружают вашу линейную компоновку с framelayout и дать силу тяжести как внизу .. его простой, чем вы думаете ... многие макеты используются только для упрощения перемещения по дизайну

<FrameLayout 
android:layout_height="fill_parent" 
android:layout_width = "fill_parent"> 

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

<Button 
android:layout_height="wrap_content" 
android:layout_width = "wrap_content" 
android:text = "btn"/> 

</LinearLayout> 

</FrameLayout> 

`

2
<LinearLayout 
    android:orientation="vertical" 
           ... > 

    <Space 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" /> 

    <SomeViewThatNeedsGoBottom 
          ... /> 

</LinearLayout> 
Смежные вопросы