2017-02-14 39 views
2

這個佈局的問題是當我定位時,文本視圖與按鈕重疊。我已經嘗試了框架佈局,重心和許多其他選項,但無濟於事。下面的圖片顯示了這個問題。我肯定需要使用RelativeLayout文字視圖與相對佈局的方向重疊

enter image description hereenter image description here

正如你所看到的,在景觀的情況下,測試的TextView被阻止。我想保持TextView垂直中心位置上的肖像模式和風景時模式下,TextView的定位BUTTON1之上,使得它不重疊。按鈕必須是保持在底部

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <TextView 
     android:id="@+id/textview" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:text="TEST" 
     android:textAlignment="center" 
     android:textSize="36sp" /> 
    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/btn1" 
     android:text="button1"/> 
    <Button 
     android:id="@+id/btn2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/btn3" 
     android:text="button 2"/> 
    <Button 
     android:id="@+id/btn3" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:text="button 3"/> 
    </RelativeLayout> 
+0

您希望的行爲是什麼? –

+0

你需要將按鈕對齊底部嗎? –

+0

@Mahmet Kologlu - 我已經在問題的行爲上添加了描述。謝謝你指出, – user859385

回答

1

添加另一個RelativeLayout作爲父母的您TextView以上btn1android:layout_height="match_parent"

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/activity_main" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@+id/btn1"> 
     <TextView 
      android:id="@+id/textview" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      android:text="TEST" 
      android:textAlignment="center" 
      android:textSize="36sp" /> 
    </RelativeLayout> 
    <Button 
     android:id="@id/btn1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/btn2" 
     android:text="button1"/> 
    <Button 
     android:id="@+id/btn2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/btn3" 
     android:text="button 2"/> 
    <Button 
     android:id="@+id/btn3" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:text="button 3"/> 
</RelativeLayout> 

另一種解決方案

添加android:layout_height="match_parent"android:layout_above="@+id/btn1"TextView,並使用中心android:gravity="center"

TextView
<RelativeLayout android:id="@+id/activity_main" 
       xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/textview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@+id/btn1" 
     android:gravity="center" 
     android:text="TEST" 
     android:textAlignment="center" 
     android:textSize="36sp"/> 

    <Button 
     android:id="@id/btn1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/btn2" 
     android:text="button1"/> 

    <Button 
     android:id="@+id/btn2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/btn3" 
     android:text="button 2"/> 

    <Button 
     android:id="@+id/btn3" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:text="button 3"/> 
</RelativeLayout> 

兩種解決方案都適用於縱向和橫向取向

+1

這對我有效! – user859385

-1

將三個按鈕插入相對佈局,該佈局應該是align-parent-bottom。然後把你的textview放在這個相對佈局的上方。 希望它能爲你工作。

+0

它不會,如果我放置在線性佈局之上,文本不是位於屏幕的中心,而只是按鈕的頂部 – user859385

+0

不,這不會使TextView中心與手機高度對齊 –

+0

如果我看到某種形式的示例,說明你在暗示什麼,這將有所幫助。我不知道我應該如何實現 – user859385

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


    <TextView 
     android:id="@+id/textview" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:text="TEST" 
     android:textAlignment="center" 
     android:textSize="36sp"/> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/textview"> 

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

      <Button 
       android:id="@+id/btn1" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="button1"/> 

      <Button 
       android:id="@+id/btn2" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="button 2"/> 

      <Button 
       android:id="@+id/btn3" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="button 3"/> 
     </LinearLayout> 
    </ScrollView> 


</RelativeLayout> 
+0

試試這個佈局 – user320676

0

您需要爲您的活動提供兩種佈局。首先是肖像模式,然後是橫向模式。要做到這一點,你需要創建一個與您的活動相同的名稱,但附加到它的地方。之後,這個XML將自動用於您的橫向模式。在這個XML中,你可以把你想要的任何邏輯視圖。不要忘記使用相同的視圖:id屬性,否則你將會有例外。希望這可以幫助。