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

标签 android android-layout material-design

我正在尝试按照 Layout - Metrics and keylines 的指南设置屏幕边距.具体来说,移动屏幕上的列表内容应有 72 dp 的边距,并与指南中显示的标题对齐:

enter image description here

由于 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)。

enter image description here

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

我错过了什么?

最佳答案

从您的 TextViewListView 中删除它:

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">

之所以关闭是因为你在padding,然后设置了margin。

另外,出于兴趣,这里是The difference between Padding and Margin .

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); 
}

为工具栏创建自定义 View

<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>

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

关于android - Material 设计 - 操作栏标题和内容的左边距不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34609920/

相关文章:

java - 赋值运算符遇到问题

java - 用于 Android 的通用图像加载器,在 Fragment 中带有 ImageView

安卓工作室 : Cant get App to install on emulator

javascript - polymer JS : How to install paper-submenu?

android - 以编程方式创建的 RippleDrawable 看起来与其 xml 对应物不同

android - ListView和行回收问题

android - 可见性设置为 "gone"的 View 是否是测量和布局过程的一部分?

android - 在为 ActionBar 设置 apptheme 时,我得到了这个奇怪的错误 red color is private。那什么意识?

android - 呈现问题 : Class could not be instantiated: - android. support.v4.widget.DrawerLayout

icons - 我在哪里可以获得 Material 图标的代码?