【问题标题】:how to align a switch to centre and a text view to right end in a linear layout?如何在线性布局中将开关对齐到中心并将文本视图对齐到右端?
【发布时间】:2017-01-23 19:20:42
【问题描述】:

我有一个水平方向的线性布局,权重总和为 3,左右两端有 2 个文本视图,中间有一个开关。我无法将文本视图对齐到右端并将开关对齐到中心。我使用了重心和重心端,但它们不起作用

<LinearLayout
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:padding="@dimen/rm_dashboard_ll_padding"
    android:weightSum="3"
    android:layout_height="wrap_content">
        <TextView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="@string/available"
             android:textColor="#82BCB4"
             android:textSize="@dimen/available_textsize"
             android:layout_weight="1"/>

        <Switch
             android:id="@+id/theSwitchId"
             android:layout_width="0dp"
             android:layout_height="wrap_content"
             android:switchMinWidth="@dimen/rm_switch_minwidth"
             android:track="@drawable/switch_bg"
             android:layout_marginStart="@dimen/margin_start_switch"
             android:thumbTint="#224e6d"
             android:layout_gravity="center"
             android:layout_marginLeft="@dimen/margin_left_switch"
             android:layout_weight="1"/>

        <TextView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="@string/unavailable"
             android:textColor="#224e6d"
             android:textSize="@dimen/unavailable_textsize"
             android:layout_gravity="end"
             android:layout_weight="1"/>
</LinearLayout>

【问题讨论】:

  • 你想达到什么目的?有截图吗?
  • 在您的代码中,我只需添加 android:gravity="right" 这个就可以了

标签: android android-layout android-linearlayout android-layout-weight


【解决方案1】:

我建议使用RelativeLayout 进行复杂的对齐。试试下面的布局模型:

<RelativeLayout>
    <TextView
        android:layout_alignParentLeft="true"
    />
    <Switch
        android:layout_alignParentCenter="true"
    />
    <TextView
        android:layout_alignParentRight="true"
    />
</RelativeLayout>

有关其他对齐类型的详细信息,请参阅RelativeLayout.LayoutParams

【讨论】:

    【解决方案2】:

    请检查此代码

    <LinearLayout
            android:layout_width="match_parent"
            android:orientation="horizontal"
            android:padding="@dimen/rm_dashboard_ll_padding"
            android:weightSum="3"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="@string/available"
                android:textColor="#82BCB4"
                android:textSize="@dimen/available_textsize"
                android:layout_weight="1"
                />
    
            <Switch
                android:id="@+id/theSwitchId"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:switchMinWidth="@dimen/rm_switch_minwidth"
                android:track="@drawable/switch_bg"
                android:layout_marginStart="@dimen/margin_start_switch"
                android:thumbTint="#224e6d"
                android:gravity="center"
                android:layout_marginLeft="@dimen/margin_left_switch"
                android:layout_weight="1"/>
    
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="@string/unavailable"
                android:textColor="#224e6d"
                android:textSize="@dimen/unavailable_textsize"
                android:gravity="end"
                android:layout_weight="1"/>
        </LinearLayout>
    

    【讨论】:

    • 它正在工作,但我想把开关放在中心@Akash
    猜你喜欢
    • 1970-01-01
    • 2014-01-14
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 2021-09-20
    • 1970-01-01
    • 2018-06-25
    • 2014-10-24
    相关资源
    最近更新 更多