2016-02-12 105 views
0

我想要两个文本和一个箭头标记到最后。所以,我试过如下:Android中使用线性布局时的布局问题

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:orientation="horizontal" 
     android:layout_weight="0.25" 
     android:gravity="center_vertical"> 

     <TextView 
      android:id="@+id/labeltext" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:gravity="center" 
      android:text="Specification"/> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:gravity="right" 
      android:src="@drawable/ic_next"/> 

    </LinearLayout> 

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

    <TextView 
      android:id="@+id/labelselected" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:text="Specification down"/> 

    </LinearLayout> 
</LinearLayout> 

但我仍然得到:

enter image description here

我想如果我们把安卓重力=“正确”的整体形象进入的权利,但这个没不动?

有人可以帮我解决这个问题吗?

+0

集重力到右的TextView –

+0

变化方向报的布局“水平“ – PeDuCKA

+0

它将layout_gravity不只是gra vity –

回答

3

注:

  1. 先放LinearLayouthorizontal方向

  2. 然后把另外2 LinearLayout里面并设置layout_weight为你的欲望

  3. 再加入什么你想要在里面。

多数民众赞成....

这里是更正后的代码

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

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="4" 
    > 
    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:gravity="center_vertical" 
     android:layout_weight="3"> 

     <TextView 
      android:id="@+id/labeltext" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:gravity="center" 
      android:text="Specification" /> 


     <TextView 
      android:id="@+id/labelselected" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:text="Specification down"/> 


    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:orientation="horizontal" 
     android:layout_weight="1" 
     android:gravity="center"> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:gravity="right" 
      android:src="@android:drawable/sym_def_app_icon"/> 

    </LinearLayout> 
</LinearLayout> 

enter image description here

+0

谢谢!这工作。 – user5287166

2

你可以这样做。

截图参考:

enter image description here

我做到了通过设置match_parent宽度TextView和分配drawableRight属性。

<?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:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.25" 
     android:gravity="center_vertical" 
     android:orientation="horizontal"> 

     <TextView 
      android:id="@+id/labeltext" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:drawableRight="@drawable/ic_next" 
      android:gravity="center_vertical" 
      android:text="Specification" /> 


    </LinearLayout> 

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

     <TextView 
      android:id="@+id/labelselected" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:text="Specification down" /> 

    </LinearLayout> 
</LinearLayout> 

希望这会帮助你。

1

而不是使用ImageView可以作为绘制你的图标设置为您TextView其宽度设置为match_parent

<TextView 
... 
android:drawableRight="@drawable/your_icon"/> 

的也是我觉得你的IDE会建议您删除您LinearLayoutTextViewImageView和使用我的解决方案,因为使用一个部件而不是三个总是更好;)

0

使用FrameLayout而不是LinearLayout像这里:

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

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.25" 
    android:gravity="center_vertical"> 

    <TextView 
     android:id="@+id/labeltext" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:text="Specification"/> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:gravity="right" 
     android:src="@drawable/ic_next" 
     android:layout_gravity="right|center_vertical"/> 

</FrameLayout> 

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

    <TextView 
     android:id="@+id/labelselected" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:text="Specification down"/> 

</LinearLayout> 

1

更改您的布局和尝试。我只是提示。将文本和图像放置在分布式layout_weight的同一行中。您可以根据需要进行调整

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:gravity="center_vertical"> 

     <TextView 
      android:id="@+id/labeltext" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="3" 
      android:text="Specification"/> 

     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="3" 
      android:src="@drawable/ic_next"/> 

    </LinearLayout> 

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

    <TextView 
      android:id="@+id/labelselected" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:text="Specification down"/> 

    </LinearLayout> 
</LinearLayout> 
1

将此替换为您的上部布局。您需要为imageView使用layout_gravity =“right”。

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:gravity="center_vertical"> 

     <TextView 
      android:id="@+id/labeltext" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:gravity="left|center" 
      android:text="Specification"/> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:gravity="right" 
      android:layout_gravity="right" 
      android:src="@drawable/ic_next"/> 

    </LinearLayout> 
0

只是为了lateltext TextView中添加重量1

<TextView 
     android:id="@+id/labeltext" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:gravity="left|center_vertical" 
     android:text="Specification" 
     android:layout_weight="1"/> 
1

使用此它会为你工作。

<?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:orientation="vertical"> 

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

     <TextView 
      android:id="@+id/labeltext" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Specification" /> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:src="@drawable/ic_next" /> 

    </RelativeLayout> 

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

     <TextView 
      android:id="@+id/labelselected" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:text="Specification down" /> 

    </LinearLayout> 
</LinearLayout>