【问题标题】:How to draw elements from the right to the left?如何从右到左绘制元素?
【发布时间】:2011-08-16 23:29:30
【问题描述】:

我想在屏幕的右侧有一个按钮,以及一个编辑文本,它占据了按钮左侧的所有位置。我该怎么办?

此 xml 将屏幕左侧的按钮打印给我,但之前什么都没有:/

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

    <Button 
        android:id="@+id/index_bouton_recherche"
        android:layout_width="45px"
        android:layout_height="45px"
        android:background="@drawable/loupe"
    />

    <EditText 
        android:id="@+id/index_champs_recherche"
        android:layout_width ="fill_parent"
        android:layout_height="wrap_content"
        android:maxLines="1" 
        android:layout_toLeftOf="@id/index_bouton_recherche"
    />

【问题讨论】:

  • android:layout_alignParentRight="true" 添加到您的按钮,就是这样......使用线性布局,您将不得不为您的其余活动创建更多布局

标签: android drawing android-relativelayout fill-parent


【解决方案1】:

当我必须这样做时,我使用LinearLayoutlayout_weight xml 属性。 这个 XML 可以解决你的问题:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
>
    <EditText 
        android:id="@+id/index_champs_recherche"
        android:layout_width ="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:maxLines="1" 
    />
    <Button 
        android:id="@+id/index_bouton_recherche"
        android:layout_width="45px"
        android:layout_height="45px"
        android:background="@drawable/loupe"
    />
</LinearLayout>

layout_width="0dp" 表示最初视图不需要任何空间。按钮是wrap_content,所以它只需要它的空间。 layout_weight 属性在之后应用,它共享布局上的剩余空间(方向的宽度=水平,垂直的高度)。默认视图权重为 0,因此为 1,EditText 将占用 100% 的剩余空间。

【讨论】:

    【解决方案2】:

    试试这个

    <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
    >
    
        <Button 
            android:id="@+id/index_bouton_recherche"
            android:layout_width="45px"
            android:layout_height="45px"
            android:background="@drawable/loupe"
        />
    
        <EditText 
            android:id="@+id/index_champs_recherche"
            android:layout_width ="fill_parent"
            android:layout_height="wrap_content"
            android:maxLines="1" 
            android:layout_toLeftOf="@id/index_bouton_recherche"
            android:layout_weight="1"
        />
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 2012-11-12
      • 1970-01-01
      • 1970-01-01
      • 2015-10-22
      • 1970-01-01
      • 2011-10-07
      • 1970-01-01
      • 1970-01-01
      • 2020-07-03
      相关资源
      最近更新 更多