2014-11-14 87 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