材质设计 - 操作栏标题和内容的左边距不匹配

2024-01-11

我正在尝试按照以下准则设置屏幕边距布局 - 指标和关键线 https://www.google.com/design/spec/layout/metrics-keylines.html。具体来说,移动屏幕上的列表内容应具有 72 dp 的边距,并与指南中所示的标题对齐:

由于 Android Studio 自动在 res/dimens.xml 中生成边距值,因此我认为可以通过添加自己的“内部”边距来使内容与标题对齐:

尺寸.xml

<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>

    <!-- 16 + 56 = 72 = "Content left margin from screen edge" -->
    <dimen name="content_horizontal_margin">56dp</dimen>
</resources>

myactivity.xml

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin">

    <TextView
        android:id="@+id/tvEditPlaylistInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="@dimen/content_horizontal_margin"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="test" />

    <ImageButton
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:src="@drawable/action_current"
        android:layout_alignBottom="@id/tvEditPlaylistInfo"
        android:layout_toRightOf="@id/tvEditPlaylistInfo"
        android:layout_marginLeft="16dp"/>

    <ListView
        android:id="@+id/lvAudioFiles"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/content_horizontal_margin"
        android:layout_below="@id/tvEditPlaylistInfo">
    </ListView>

</RelativeLayout>

但不知怎的,我的内容“关闭”了大约 8dp(模拟器,Lollipop 22)。

我正在与父母一起使用自定义主题Theme.AppCompat.Light.DarkActionBar但我只改变了颜色,所以我认为这不是我的问题的根源。

我缺少什么?


将其从您的TextView and ListView:

android:layout_marginLeft="@dimen/content_horizontal_margin"

您的父标签已经指定了填充应该是什么:

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
     android:paddingRight="@dimen/activity_horizontal_margin">

之所以关闭是因为你正在填充,然后设置边距。

另外,只是为了兴趣,这里是内边距和边距的区别 https://stackoverflow.com/a/25520320/4180965.

UPDATE

根据与 @0X0nosugar 的讨论,我们已经得出以下代码有助于实现您的目标:

为了更改 ActionBar,应将以下内容添加到 onCreate() 中:

ActionBar actionBar = getSupportActionBar(); 

if (actionBar != null) 
{ 
    View customView = getLayoutInflater().inflate(R.layout.custom_action_bar, null); 
    actionBar.setDisplayShowTitleEnabled(false); 
    actionBar.setDisplayShowCustomEnabled(true); 
    actionBar.setCustomView(customView); 
}

为工具栏创建自定义视图

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:background="@color/accent_deeporangeA400" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="MyMusicApp" 
    android:id="@+id/ab_title" 
    android:layout_centerVertical="true" 
    android:layout_alignParentLeft="true" 
    android:layout_marginLeft="@dimen/activity_horizontal_margin" 
    android:textAppearance="?android:attr/textAppearanceLarge"/> 

</RelativeLayout>

这应该可以帮助您实现目标!你的研究很好!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

材质设计 - 操作栏标题和内容的左边距不匹配 的相关文章

随机推荐