我正在尝试按照 Layout - Metrics and keylines 的指南设置屏幕边距.具体来说,移动屏幕上的列表内容应有 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
和 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">
之所以关闭是因为你在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/