2016-09-07 56 views
0

我正面臨着android佈局的問題。 我有一個textview的相對佈局和一個圖像重疊,我想添加兩個按鈕在視圖的底部,一個接近其他,並填寫視圖的所有寬度。 manifest.xmlRelativeLayout底部的兩個接近其他的按鈕 - Android

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 
    android:orientation="vertical" 
    android:id="@+id/RecordAccelDataLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <ImageView 
     android:layout_gravity="center" 
     android:longClickable="false" 
     android:layout_width="283dp" 
     android:layout_height="418dp" 
     android:src="@mipmap/bg_thand" 
     android:adjustViewBounds="false" 
     android:alpha="0.1" /> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/rectangle"> 

     <TextView 
      android:textSize="20sp" 
      android:gravity="center_horizontal" 
      android:id="@+id/rec_progress_text" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="30dp" 
      android:text="@string/title_activity_record_accel_data" 
      /> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 

      <com.adrenergic.tremorsense.circleGraph 
       android:layout_gravity="center" 
       android:id="@+id/progressgraph" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerInParent="true"/> 

      <TextView 
       android:textSize="20sp" 
       android:id="@+id/accelText" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/recording" 
       android:layout_centerHorizontal="true" 
       android:layout_centerVertical="true" 
       android:textIsSelectable="true" 
       /> 

      <Button 
       android:textColor="#ffffff" 
       android:layout_width="100dp" 
       android:id="@+id/back" 
       android:layout_height="50dp" 
       android:text="@string/back" 
       android:layout_gravity="center" 
       android:layout_marginBottom="3dip" 
       android:onClick="goBack" 
       android:layout_alignParentBottom="true" 
       android:layout_centerHorizontal="true" /> 

      <Button 
       android:textColor="#ffffff" 
       android:layout_width="100dp" 
       android:layout_height="50dp" 
       android:layout_gravity="center" 
       android:text="@string/stats" 
       android:onClick="goBack" 
       android:id="@+id/stats" 
       android:layout_marginBottom="3dip" 
       android:layout_centerHorizontal="true" 
       android:layout_alignParentBottom="true" 
       android:layout_toRightOf="@+id/back"/> 
     </RelativeLayout> 
    </LinearLayout> 
</FrameLayout> 

佈局以這種方式出現。

enter image description here

我想,這兩個按鈕出現在相同的位置,但centered和他們填寫同樣在寬度的圖。

希望你能幫助我。

+0

你爲什麼不嘗試下用同樣的'weight'另一個線性佈局的按鈕?你試過了嗎?希望它可以幫助。 –

+0

是的,我已經嘗試,但線性佈局出現在視圖的頂部 –

+0

我想說的是由Raghavendra和Ramesh庫馬爾以及Sourav Kanta –

回答

2

你可以修改你的按鈕佈局impl。與下面的示例並嘗試。

<LinearLayout 
       android:orientation="horizontal" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignParentBottom="true" 
       android:weightSum="1"> 
      <Button 
       android:textColor="#ffffff" 
       android:layout_width="0dp" 
       android:id="@+id/back" 
       android:layout_weight="0.5" 
       android:layout_height="50dp" 
       android:text="@string/back" 
       android:layout_gravity="center" 
       android:layout_marginBottom="3dip" 
       android:onClick="goBack"/> 

      <Button 
       android:textColor="#ffffff" 
       android:layout_width="0dp" 
       android:layout_height="50dp" 
       android:layout_weight="0.5" 
       android:layout_gravity="center" 
       android:text="@string/stats" 
       android:onClick="goBack" 
       android:id="@+id/stats" 
       android:layout_marginBottom="3dip"/> 
      </LinearLayout> 
+1

太棒了!非常感謝你 –

+0

@ParideLetizia很高興!它幫助:) – Raghavendra

0

嗨,你可以把按鈕線性佈局和調整父底部:

<?xml version="1.0" encoding="utf-8"?> 

<ImageView 
    android:layout_width="283dp" 
    android:layout_height="418dp" 
    android:layout_gravity="center" 
    android:adjustViewBounds="false" 
    android:alpha="0.1" 
    android:longClickable="false" 
    android:src="@mipmap/bg_thand" /> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/rectangle" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/rec_progress_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="30dp" 
     android:gravity="center_horizontal" 
     android:text="@string/title_activity_record_accel_data" 
     android:textSize="20sp" /> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <com.adrenergic.tremorsense.circleGraph 
      android:id="@+id/progressgraph" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:layout_gravity="center" /> 

     <TextView 
      android:id="@+id/accelText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_centerVertical="true" 
      android:text="@string/recording" 
      android:textIsSelectable="true" 
      android:textSize="20sp" /> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:orientation="horizontal"> 

      <Button 
       android:id="@+id/back" 
       android:layout_width="0dp" 
       android:layout_height="50dp" 
       android:layout_alignParentBottom="true" 
       android:layout_centerHorizontal="true" 
       android:layout_gravity="center" 
       android:layout_marginBottom="3dip" 
       android:layout_weight="1" 
       android:onClick="goBack" 
       android:text="@string/back" 
       android:textColor="#ffffff" /> 

      <Button 
       android:id="@+id/stats" 
       android:layout_width="0dp" 
       android:layout_height="50dp" 
       android:layout_alignParentBottom="true" 
       android:layout_centerHorizontal="true" 
       android:layout_gravity="center" 
       android:layout_marginBottom="3dip" 
       android:layout_toRightOf="@+id/back" 
       android:layout_weight="1" 
       android:onClick="goBack" 
       android:text="@string/stats" 
       android:textColor="#ffffff" /> 
     </LinearLayout> 
    </RelativeLayout> 
</LinearLayout> 

檢查這會有所幫助。

0

android:weight您可能正在尋找。將兩個按鈕放在一個線性佈局中,併爲它們添加android:weight = 1。一個例子是:

<LinearLayout 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
> 

    <Button android:id="@+id/button1" 
      ... 
      android:layout_weight="1"/> 

    <View 
      android:id="@+id/space" 
      ... 
      android:layout_weight=".1"/> 

    <Button android:id="@+id/button2" 
      ... 
      android:layout_weight="1"/> 

</LinearLayout> 
0

您需要在中心底部添加spacer並對齊兩個按鈕是spacer這樣的:

<RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 

      <com.adrenergic.tremorsense.circleGraph 
       android:layout_gravity="center" 
       android:id="@+id/progressgraph" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerInParent="true"/> 

      <TextView 
       android:textSize="20sp" 
       android:id="@+id/accelText" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:layout_centerVertical="true" 
       android:textIsSelectable="true" 
       /> 
      //Aligh Space in center with align to bottom 
       and set relation of two button to this spacer 
      <Space 
       android:layout_width="1px" 
       android:layout_height="1px" 
       android:layout_above="@+id/back" 
       android:layout_centerHorizontal="true" 
       android:layout_alignParentBottom="true" 
       android:id="@+id/space"/> 

      <Button 
       android:textColor="#ffffff" 
       android:layout_width="100dp" 
       android:id="@+id/back" 
       android:layout_height="50dp" 
       android:text="Back" 
       android:layout_gravity="center" 
       android:onClick="goBack" 
       android:layout_alignParentBottom="true" 
       android:layout_alignStart="@+id/accelText"/> 

      <Button 
       android:textColor="#ffffff" 
       android:layout_width="100dp" 
       android:layout_height="50dp" 
       android:layout_gravity="center" 
       android:text="Status" 
       android:onClick="goBack" 
       android:id="@+id/stats" 
       android:layout_alignParentBottom="true" 
       android:layout_toStartOf="@+id/space"/> 


     </RelativeLayout> 
0

使用內部Linear Layout添加Button使這個layout變化並用weightandroid:gravity="bottom"android:orientation="horizontal"填充。

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/RecordAccelDataLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <ImageView 
     android:layout_width="283dp" 
     android:layout_height="418dp" 
     android:layout_gravity="center" 
     android:adjustViewBounds="false" 
     android:alpha="0.1" 
     android:longClickable="false" 
     android:src="@drawable/common_ic_googleplayservices" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/common_ic_googleplayservices" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/rec_progress_text" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="30dp" 
      android:gravity="center_horizontal" 
      android:text="Progress" 
      android:textSize="20sp" /> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <com.adrenergic.tremorsense.circleGraph 
       android:id="@+id/progressgraph" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerInParent="true" 
       android:layout_gravity="center" /> 

      <TextView 
       android:id="@+id/accelText" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:layout_centerVertical="true" 
       android:text="REcording" 
       android:textIsSelectable="true" 
       android:textSize="20sp" /> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="bottom" 
       android:orientation="horizontal" 
       android:weightSum="1"> 

       <Button 
        android:id="@+id/back" 
        android:layout_width="100dp" 
        android:layout_height="50dp" 
        android:layout_weight="0.50" 
        android:onClick="goBack" 
        android:text="Back" 
        android:textColor="#ffffff" /> 

       <Button 
        android:id="@+id/stats" 
        android:layout_width="100dp" 
        android:layout_height="50dp" 
        android:layout_weight="0.50" 
        android:onClick="goBack" 
        android:text="Stats" 
        android:textColor="#ffffff" /> 

      </LinearLayout> 
     </RelativeLayout> 
    </LinearLayout> 
</FrameLayout> 

enter image description here

+0

欣賞答案與upvote如果某個地方回答對OP有意義。 –

相關問題