2017-07-03 80 views
1

有沒有一種方法可以在Android上擁有全屏行的元素? 我試圖用table,GridView和linear來做,但是我現在很努力地工作,找不到幫助我的帖子。Android簡單的百分比佈局?

我剛纔提出的想什麼,我做的小圖片: http://i.imgur.com/W0oTyaC.pngenter image description here

總之一點:

  • 標籤和按鈕應該只是大如他們的內容和配合到列中最大的標籤

  • 該按鈕應該粘在右側

  • 標籤應該堅持左側

  • 文本框應該只需填寫標籤/按鈕之間的多餘的空間,但在更大的內容的情況下,它不應該「踢出」右邊的按鈕。 ..

我最後的嘗試是GridView,它看起來是一種可以接受的,但不是真的。

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#FFFFFF"> 

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

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

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Add nutrition part" 
       android:id="@+id/textView" 
       android:layout_column="0" 
       android:layout_row="0" 
       android:layout_columnSpan="1" 
       android:layout_rowSpan="1" /> 

      <TextView 
       android:text="Selected part" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/textView15" 
       android:layout_column="0" 
       android:layout_row="1" 
       android:layout_columnSpan="1" 
       android:layout_rowSpan="1" /> 

      <EditText 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:inputType="text" 
       android:text="" 
       android:enabled="false" 
       android:id="@+id/dialogMakeMealPartNutrition" 
       android:layout_column="1" 
       android:layout_row="1" 
       android:layout_columnSpan="1" 
       android:layout_rowSpan="1" 
       android:layout_gravity="fill_horizontal" /> 

      <Button 
       android:text="Find nutrition" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/buttonMakeMealPartFindNutrition" 
       android:layout_column="2" 
       android:layout_row="1" 
       android:layout_columnSpan="1" 
       android:layout_rowSpan="1" /> 

      <TextView 
       android:text="Gram" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/textView17" 
       android:layout_column="0" 
       android:layout_row="2" 
       android:layout_columnSpan="1" 
       android:layout_rowSpan="1" /> 

      <EditText 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:inputType="numberDecimal" 
       android:id="@+id/dialogMakeMealPartGram" 
       android:layout_column="1" 
       android:layout_row="2" 
       android:layout_columnSpan="1" 
       android:layout_rowSpan="1" 
       android:layout_gravity="fill_horizontal" /> 

      <Button 
       android:text="Abort" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/dialogMakeMealPartButtonAbort" 
       android:layout_column="0" 
       android:layout_row="3" 
       android:layout_columnSpan="1" 
       android:layout_rowSpan="1" /> 

      <Button 
       android:text="Add" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/dialogMakeMealPartButtonAdd" 
       android:layout_column="1" 
       android:layout_row="3" 
       android:layout_columnSpan="1" 
       android:layout_rowSpan="1" /> 

     </GridLayout> 

    </ScrollView> 

</RelativeLayout> 

編輯:

的ConstraintLayout她對我的目的相當不錯的工作 - 感謝很好的提示盧卡:

Task description

代碼現在看起來是這樣的:

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

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

     <android.support.constraint.ConstraintLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <TextView 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:text="Add nutrition part" 
       android:id="@+id/headline" 
       app:layout_constraintEnd_toEndOf="parent" 
       app:layout_constraintStart_toStartOf="parent" 
       app:layout_constraintTop_toTopOf="parent" /> 

      <TextView 
       android:text="Selected part" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/textView15" 
       app:layout_constraintStart_toStartOf="parent" 
       app:layout_constraintTop_toTopOf="@+id/buttonMakeMealPartFindNutrition" /> 

      <EditText 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:inputType="text" 
       android:text="" 
       android:enabled="false" 
       android:id="@+id/dialogMakeMealPartNutrition" 
       app:layout_constraintStart_toEndOf="@+id/textView15" 
       app:layout_constraintEnd_toStartOf="@+id/buttonMakeMealPartFindNutrition" 
       app:layout_constraintTop_toTopOf="@+id/buttonMakeMealPartFindNutrition" /> 

      <Button 
       android:text="Find nutrition" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/buttonMakeMealPartFindNutrition" 
       app:layout_constraintTop_toBottomOf="@+id/headline" 
       app:layout_constraintStart_toEndOf="@+id/dialogMakeMealPartNutrition" 
       app:layout_constraintEnd_toEndOf="parent" /> 

      <TextView 
       android:text="Gram" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/textView17" 
       app:layout_constraintStart_toStartOf="parent" 
       app:layout_constraintTop_toBottomOf="@+id/dialogMakeMealPartNutrition" /> 

      <EditText 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:inputType="numberDecimal" 
       android:id="@+id/dialogMakeMealPartGram" 
       app:layout_constraintStart_toStartOf="@+id/dialogMakeMealPartNutrition" 
       app:layout_constraintEnd_toEndOf="@+id/dialogMakeMealPartNutrition" 
       app:layout_constraintTop_toBottomOf="@+id/dialogMakeMealPartNutrition" /> 

      <Button 
       android:text="Abort" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/dialogMakeMealPartButtonAbort" 
       app:layout_constraintStart_toStartOf="parent" 
       app:layout_constraintTop_toBottomOf="@+id/dialogMakeMealPartGram" /> 

      <Button 
       android:text="Add" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/dialogMakeMealPartButtonAdd" 
       app:layout_constraintStart_toEndOf="@+id/dialogMakeMealPartButtonAbort" 
       app:layout_constraintTop_toBottomOf="@+id/dialogMakeMealPartGram" /> 

     </android.support.constraint.ConstraintLayout> 

    </ScrollView> 

</RelativeLayout> 
+0

您使用的網格佈局或任何其他 – Raj

+0

沒有,didtn工作實現目標(或我不知道怎麼樣,我猜),我嘗試了網格佈局,但這個按鈕是滾動出來的,我不能讓文本框填充剩餘空間 – Leorekk

+0

'...只填充剩餘空間'然後使用'match_parent'而不是'wrap_content' –

回答

0

你可以使用一個ConstraintLayout來實現這個,here你可以找到它的解釋。

您的佈局文件應該是這樣的:

<android.support.constraint.ConstraintLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="Add nutrition part" 
     app:layout_constraintEnd_toEndOf="parent" 
     app:layout_constraintStart_toStartOf="parent" 
     app:layout_constraintTop_toTopOf="parent"/> 

    <TextView 
     android:id="@+id/textView15" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Selected part" 
     app:layout_constraintStart_toStartOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/textView"/> 

    <EditText 
     android:id="@+id/dialogMakeMealPartNutrition" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:enabled="false" 
     android:inputType="text" 
     app:layout_constraintStart_toEndOf="@+id/textView15" 
     app:layout_constraintEnd_toStartOf="@+id/buttonMakeMealPartFindNutrition" 
     app:layout_constraintTop_toBottomOf="@+id/textView" 
     android:text=""/> 

    <Button 
     android:id="@+id/buttonMakeMealPartFindNutrition" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_constraintTop_toBottomOf="@+id/textView" 
     app:layout_constraintStart_toEndOf="@+id/dialogMakeMealPartNutrition" 
     app:layout_constraintEnd_toEndOf="parent" 
     android:text="Find nutrition"/> 

    ... 

</android.support.constraint.ConstraintLayout>