【问题标题】:How to reduce material.switchmaterial.SwitchMaterial end margin in android?如何减少android中的material.switchmaterial.SwitchMaterial结束边距?
【发布时间】:2021-04-11 21:16:04
【问题描述】:

我在左边空白处使用了 "com.google.android.material.switchmaterial.SwitchMaterial"。如何减少?

我需要对齐所有人文本。我没有添加开关的结束边距或填充,只有我设置的父布局边距。在我的 XML 代码和样式下方。请提供您的建议以减少该边际,谢谢。

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp">

    <RelativeLayout
        android:id="@+id/who_can"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp">

        <com.google.android.material.textview.MaterialTextView
            style="@style/TextBlackRegular"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_toStartOf="@+id/video_view_result"
            android:singleLine="true"
            android:text="@string/who_can_view"
            android:textSize="@dimen/text_small"
            android:visibility="visible" />

        <com.google.android.material.textview.MaterialTextView
            android:id="@+id/video_view_result"
            style="@style/TextBlackRegular"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:singleLine="true"
            android:text="Everyone"
            android:textSize="@dimen/text_small" />

    </RelativeLayout>


    <com.google.android.material.switchmaterial.SwitchMaterial
        android:id="@+id/save_switch"
        style="@style/SwitchButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/who_can"
        android:checked="true"
        android:text="@string/save_gallery"
        android:textSize="@dimen/text_small"
        app:trackTint="@color/colorSecondaryText" />
</RelativeLayout>


    <style name="SwitchButtonStyle" parent="Widget.MaterialComponents.CompoundButton.Switch">
            <item name="thumbTint">@drawable/switch_selector</item>
            <item name="android:fontFamily">@font/font_regular</item>
            <item name="android:textColor">@color/colorBlack</item>
        </style>

【问题讨论】:

    标签: android material-design material-components-android material-components android-switch


    【解决方案1】:

    在您的relativeLayout/topRelativeLayout 中,您添加了边距。这意味着您想从各个方面获利。这就是它发生的原因

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp">
    

    删除android:layout_margin="10dp"。添加margin_topmargin_right你想做的事。

    【讨论】:

    • 我已经删除了那个边距,谁可以观看我的视频,每个人都可以观看并保存到图库中,所有文本都与左侧和右侧对齐,但仍会切换结束边距显示。
    • @RaJ 我怎么说谁可以观看您的视频?你刚刚要求margin。而且,如果不给我们java/kotlin 文件,我们怎么能这么说。并且,现在检查您添加margin_end 的位置。比删除它。然后,结束margin 不会显示
    【解决方案2】:

    您应该为 switch width 提供换行内容,或者将其放在新的相对布局中

    【讨论】:

      【解决方案3】:

      如果在移除后显示您的结束边距

      <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_margin="10dp">
      

      然后

      <com.google.android.material.switchmaterial.SwitchMaterial
              android:id="@+id/save_switch"
              style="@style/SwitchButtonStyle"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_below="@+id/who_can"
              android:checked="true"
              android:text="@string/save_gallery"
              android:textSize="@dimen/text_small"
              app:trackTint="@color/colorSecondaryText" />
      

      SwitchButtonStyle 的更改为您提供了一些默认值,如果您共享整个文件,我们很高兴

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-10-05
        • 1970-01-01
        • 1970-01-01
        • 2011-05-01
        • 1970-01-01
        • 2012-11-01
        相关资源
        最近更新 更多