2014-11-14 91 views
1

在android中有百分比寬度的任何選項嗎?我試圖將我的2行設置爲40%,將文本設置爲20%,但我真的不知道如何實現此目標。我現在有固定的寬度。線性佈局中的線條寬度百分比

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

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="14dp" 
       android:id="@+id/line2" 
       android:background="@drawable/line" 
       android:layout_gravity="center_horizontal" 
       android:contentDescription="@string/line" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:text="@string/or" 
       android:textAllCaps="true" 
       android:gravity="center" 
       android:id="@+id/textView" 
       android:layout_marginLeft="10dp" 
       android:layout_marginRight="10dp" /> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:id="@+id/imageView" 
       android:background="@drawable/line" 
       android:layout_gravity="center_horizontal" 
       android:layout_weight="2.18" 
       android:contentDescription="@string/line" /> 

     </LinearLayout> 

這是機器人工作室屏幕更好的理解:screen

+0

您是否嘗試使用小部件的重量?這將有助於 – Amsheer 2014-11-14 13:01:51

+0

使用新的百分比支持庫,現在可以使用百分比佈局。 [查看此鏈接](https://github.com/JulienGenoud/android-percent-support-lib-sample) – kleinsenberg 2015-07-20 22:18:26

回答

1

可以使用屬性android:layout_weight了點。 像這樣:

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

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="14dp" 
      android:id="@+id/line2" 
      android:background="@drawable/line" 
      android:layout_gravity="center_horizontal" 
      android:layout_weight="2" 
      android:contentDescription="@string/line" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="@string/or" 
      android:textAllCaps="true" 
      android:gravity="center" 
      android:layout_weight="1" 
      android:id="@+id/textView" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" /> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:id="@+id/imageView" 
      android:background="@drawable/line" 
      android:layout_gravity="center_horizontal" 
      android:layout_weight="2" 
      android:contentDescription="@string/line" /> 

    </LinearLayout> 

請注意,我在LinearLayout和我在LinearLayout本身改變android:weightSum到5

+0

但layout_width設置爲120 dp,我無法將其設置爲固定大小,因爲它不會在更大的屏幕上全尺寸,或? – mayyyo 2014-11-14 13:06:49

+0

@mayyyo,請嘗試與layout_width =「wrap_content」 - 讓我知道它是否工作。我目前的工作場所沒有測試Android的能力。 – MarchingHome 2014-11-14 13:11:25

+0

我已經試過了,但後來我沒有看到任何一行 – mayyyo 2014-11-14 13:21:39

0

嘗試這樣增加了每個元素的權重,

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

     <ImageView 
      android:layout_width="0dp" 
      android:layout_height="14dp" 
      android:id="@+id/line2" 
      android:background="@drawable/line" 
      android:layout_gravity="center_horizontal" 
      android:layout_weight="0.40" 
      android:contentDescription="@string/line" /> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="@string/or" 
      android:textAllCaps="true" 
      android:gravity="center" 
      android:layout_weight="0.20" 
      android:id="@+id/textView" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" /> 

     <ImageView 
      android:layout_width="0dp" 
      android:layout_height="14dp" 
      android:id="@+id/imageView" 
      android:background="@drawable/line" 
      android:layout_gravity="center_horizontal" 
      android:layout_weight="0.40" 
      android:contentDescription="@string/line" /> 

    </LinearLayout> 
+0

複製我的答案,這是剛剛發佈前你的1.5分鐘:) – MarchingHome 2014-11-14 13:00:19

+0

是的。我已經刪除了我的評論 – Amsheer 2014-11-14 13:03:05

+0

@MarchingHome我認爲如果這個人重複你的代碼,然後在你的imageView你的代碼是'android:layout_width =「120dp」'但回答的人代碼是不同的。那麼你怎麼說它是重複的 – 2014-11-14 13:06:35