android线性布局将两个项目放在一起

时间:2014-03-17 10:27:50

标签: android android-layout android-linearlayout

我有以下布局:

<LinearLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >

   <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" 
            >
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Status:"
    android:layout_gravity="left"
    android:textAppearance="?android:attr/textAppearanceMedium" />
  <ImageView
                android:id="@+id/txcpluss_imageView_refresh"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_gravity="right"
                android:clickable="true"
                android:contentDescription="icon"
                android:gravity="right"
                android:src="@drawable/refresh_big" />
  </LinearLayout>
 </LinearLayout>

但我的刷新图标不在我想要的右上角。它就在文本视图旁边。

如何让文本视图位于左侧,右侧的刷新图标位于左侧?

2 个答案:

答案 0 :(得分:8)

使用以下代码

<LinearLayout 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"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_weight="1"
        android:text="Status:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageView
        android:id="@+id/txcpluss_imageView_refresh"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="right"
        android:clickable="true"
        android:contentDescription="icon"
        android:gravity="right"
        android:src="@drawable/refresh_big" />
</LinearLayout>

我为文字视图添加了重量。

答案 1 :(得分:0)

同样的想法,但均匀分割权重更有意义

   <LinearLayout 

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <Button
        android:id="@+id/button_capture"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:layout_weight="0.5"
        android:background="?android:attr/selectableItemBackground"
        android:onClick="snapIt"
        android:text="@string/Capture" />

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:background="?android:attr/selectableItemBackground"
        android:onClick="snapIt"
        android:text="@string/Capture" />
   </LinearLayout>