2016-12-02 57 views
0

我要實現以下佈局:
enter image description here如何平躺這些視圖?

我做到了,如下所示:

<?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:layout_marginTop="10dp" 
    android:orientation="horizontal" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:orientation="vertical" 
     android:layout_marginRight="20dp" 
    > 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="LABEL" 
      android:textSize="14sp" 
      android:layout_marginBottom="5dp" 
     /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@color/white" 
      android:padding="10dp" 
      android:text="Text Box 1" 
      android:textSize="14sp" 
     /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
    > 
     <LinearLayout android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:orientation="vertical" 
     > 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="LABEL 2" 
       android:layout_marginBottom="5dp" 
       android:textSize="14sp" 
      /> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="horizontal" 
      > 
       <TextView 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight="1" 
        android:gravity="center" 
        android:text="BLA-1" 
        android:background="@color/white" 
        android:textSize="14sp" 
       /> 

       <TextView android:layout_width="wrap_content" 
          android:layout_height="match_parent" 
          android:layout_marginLeft="10dp" 
          android:layout_marginRight="10dp" 
          android:gravity="center|bottom" 
          android:text="and" 
          android:textSize="14sp" 
       /> 

       <TextView 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight="1" 
        android:gravity="center" 
        android:text="BLA-2" 
        android:background="@color/white" 
        android:textSize="14sp" 
       /> 


      </LinearLayout> 

     </LinearLayout> 

    </LinearLayout> 

</LinearLayout> 

基本上我用LinearLayouts和重力。
它的作品,但它似乎有點複雜,我注意到,當我增加and文本的左右邊距,文本BLA-1BLA-2被截斷。
有沒有更好的方法呢?
我想我不能使用相對佈局,因爲我無法讓視圖在屏幕上顯示這些尺寸,對不對?

UPDATE:
還與我的做法在TextViews任何一種填充切斷文本

+0

查看我的回答如下,讓我知道,如果你面對的任何問題。 –

+0

你檢查了我的答案嗎?它工作與否?讓我知道如果您遇到任何問題先生。 –

回答

0

這看起來一樣的屏幕截圖(只自定義顏色)

<?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:orientation="horizontal" 
    android:padding="16dp"> 

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

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="LABEL" /> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="#FFF" 
      android:hint="Text Box 1" 
      android:padding="4dp" 
      android:textColorHint="#757575" /> 

    </LinearLayout> 

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

     <TextView 
      android:layout_width="wrap_content" 

      android:layout_height="wrap_content" 
      android:text="LABEL2" /> 

     <LinearLayout 

      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 

      <EditText 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="#FFF" 
       android:hint="BLA-1" 
       android:padding="4dp" 
       android:textColorHint="#757575" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="4dp" 
       android:layout_marginRight="4dp" 
       android:text="and" /> 

      <EditText 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="#FFF" 
       android:hint="BLA-2" 
       android:padding="4dp" 
       android:textColorHint="#757575" /> 
     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 
+0

不,這是不一樣的。我希望textview有更多的空間。隨着你的片段,他們被「縮小」在一起。我測試了它 – Jim

+0

嘿吉姆,哪個TextView應該有更多的空間?你能不能告訴我他們是如何在屏幕截圖上縮小的? – stasbar

0

我想LinearLayout是一個很好的選擇,儘管如果你想保持權重很複雜。 您應該將您所有的TextView s更改爲wrap_content,以便它們不會被切斷。 如果你仍然想避免空間你可以用每個TextViewLinearLayoutFrameLayout這樣的:

<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:orientation="vertical"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="LABEL2" /> 
</LinearLayout> 
+0

如果我使用'wrap_content',那麼文本瀏覽「縮小」我想讓它們一路擴展,如截圖 – Jim

0

在這裏,您可以在使用Linear Layout得到。看看下面xml佈局正是你在找什麼。

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:layout_marginTop="5dp" 
     android:orientation="horizontal" 
     android:weightSum="3" 
     tools:ignore="DisableBaselineAlignment"> 

     <LinearLayout 
      android:id="@+id/llOne" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1.5" 
      android:orientation="vertical"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="5dp" 
       android:text="LABEL" 
       android:textSize="14sp" /> 

      <EditText 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@color/white" 
       android:hint="textbox1" 
       android:padding="10dp" 
       android:textSize="14sp" /> 

     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/llTwo" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.6" 
      android:orientation="vertical"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="5dp" 
       android:text="LABEL" 
       android:textSize="14sp" /> 

      <EditText 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@color/white" 
       android:hint="BLA1" 
       android:padding="10dp" 
       android:textSize="14sp" /> 

     </LinearLayout> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center|bottom" 
      android:layout_marginBottom="5dp" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_weight="0.3" 
      android:text="and" 
      android:textSize="14sp" /> 

     <LinearLayout 
      android:id="@+id/llThree" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center|bottom" 
      android:layout_weight="0.6" 
      android:orientation="vertical"> 


      <EditText 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@color/white" 
       android:hint="BLA2" 
       android:padding="10dp" 
       android:textSize="14sp" /> 

     </LinearLayout> 

    </LinearLayout> 

</RelativeLayout> 

enter image description here

+0

這種方法與我的最終結果相同。它包裝了文字。另外你如何選擇權重不是很清楚 – Jim

+0

@Jim你可以在包裝文本時顯示輸出。權重行爲是基於*權重和*保持的。 –