2017-09-13 78 views
3

假設我有以下的屏幕布局: enter image description here如何使視圖對齊底部

我有一個AdView在頁面的底部。我在它上面也有一個浮動按鈕。整個佈局在RelativeLayout。當我將AdView設置爲GONE時,無法找到懸浮按鈕。我將浮動按鈕的佈局參數設置爲高於AdView,並且是alignParentRight。即使在將AdView的可見性設置爲GONE之後,確保浮動按鈕仍保留在屏幕右下角的最佳做法是什麼?

如果我把兩個視圖LinearLayout內,結果將是這一個: enter image description here

這裏的XML代碼:

<LinearLayout 
     android:id="@+id/cpnt" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:background="@color/alkitabfeedback_step_pager_selected_tab_color" 
     android:orientation="vertical"> 

     <android.support.design.widget.FloatingActionButton 
      android:id="@+id/fab_play_audio" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 

      android:layout_alignParentRight="false" 

      android:layout_gravity="right" 
      android:layout_margin="10dp" 
      android:src="@drawable/ic_fab_audio" 
      android:visibility="visible" /> 

     <com.google.android.gms.ads.AdView 
      android:id="@+id/adView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="false" 
      android:background="@android:color/white" 
      android:visibility="visible" 
      ads:adSize="SMART_BANNER" 
      ads:adUnitId="@string/ad_unit_id" /> 
    </LinearLayout> 
+0

你會使用一個CoordinatorLayout這裏 –

回答

1

我覺得你應該把你的AdViewFloatActionButtonLinearLayout(設置方向=垂直)。 FloatActionButton位於LinearLayout的右側,AdView的位置在width =「match_parent」LinearLayout將被放置在屏幕的底部,就像您完成了AdView一樣。

<?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="wrap_content" 
    android:orientation="vertical"> 

    <android.support.design.widget.FloatingActionButton 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="right"/> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="100dp" 
     android:background="#00ff00"/> 
</LinearLayout> 

或者使用ConstraintLayout

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" /> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="100dp" 
     android:background="#00ff00" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/button" /> 
</android.support.constraint.ConstraintLayout> 
+0

我已更新我的問題。我在LinearLayout中仍然存在問題 –

+0

你能告訴我你的'LinearLayout'包含'FloatActionButton'和'AdView'嗎? –

+0

我已更新代碼 –

0

而不是設置能見度GONE,你可以嘗試將其高度調整爲0.

adview.getLayoutParams().height = 0;

+0

或者設置INVISIBLE –

+1

的可見性如果我將其設置爲不可見,則屏幕底部會有一個空白區域。 –

0

設置按鈕PARAMS到:

alignParentRight=true 
layout_above="+id/adView" 
+0

這是我目前的設置。並且這不起作用 –

1

使用線性佈局和android:重力= 「底」 來設置此佈局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
    android:gravity="bottom" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<android.support.design.widget.FloatingActionButton 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="bottom"/> 

<View 
    android:layout_width="match_parent" 
    android:layout_height="100dp" 
    android:background="#00ff00"/> 
</LinearLayout>