【问题标题】:placing an element to the right in a linear layout in android在android中以线性布局将元素放在右侧
【发布时间】:2012-03-07 08:24:43
【问题描述】:

我想在视图的右侧放置一个图像。为此,我很想使用类似的东西

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent" >

...some other elements

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >

    <ImageView
        android:id="@+id/imageIcon"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"            
        android:layout_gravity="right"
        android:src="@drawable/image_icon" >
    </ImageView>
</LinearLayout>

...
</RelativeLayout>

以上似乎将图像放在中心的某个位置。无论我们处于纵向还是横向模式,如何确保图像正确对齐?

谢谢!

【问题讨论】:

    标签: android alignment android-linearlayout


    【解决方案1】:

    以下似乎有效。两个变化 1. 按照 Zortkun 的建议使用方向 = 垂直 2.在layout_width中使用wrap_content。

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    
        <ImageView
            android:id="@+id/settingsIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:src="@drawable/settings_icon" >
        </ImageView>
    </LinearLayout>
    

    【讨论】:

    • android:orientation="vertical" 一直丢失.. :) 删除它,你会看到..
    【解决方案2】:

    如果您只在 Linearlayout 中使用一个视图,则不需要 Linearlayout。

    反正就在这里,

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent" >
    
    //...some other elements
    
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="right">
    
        <ImageView
            android:id="@+id/imageIcon"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"            
            android:layout_gravity="right"
            android:src="@drawable/image_icon" >
        </ImageView>
    </LinearLayout>
    
    </RelativeLayout>
    

    【讨论】:

      【解决方案3】:

      LinearLayout 的默认方向是水平的。确保线性布局的方向设置为 Vertical 否则您无法水平放置东西。

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

      【讨论】:

      • 感谢您的超级快速响应 - 但很抱歉 - 这不起作用。
      • 你的linearlayout里还有什么?
      • 没有别的了。其余的东西在外部相对布局内 - 基本上我想要一排只有一个图像在右侧。我当然可以使用左边距(有效),但我想使用对齐,所以我不必担心纵向/横向模式......
      • 是的,在这种情况下您不应该使用边距。这应该很容易。您能否发布您的整个 xml,以便我们进行更好的测试?
      • 嗨 Zortkun - 谢谢你的回答。我不需要“中心}正确”。我以为我以前试过这个。也许“方向=垂直”是新事物。谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多