覆盖操作栏 - Android Studio教程

时间:2015-05-31 11:02:13

标签: android android-actionbar overlay

使用Android教程http://developer.android.com/training/basics/actionbar/overlaying.html

时遇到一个小问题

我创建了一个自定义操作栏主题,其中android:windowActionBarOverlay属性已设置为true。

<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
    parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowActionBarOverlay">true</item>

    <!-- Support library compatibility -->
    <item name="windowActionBarOverlay">true</item>
</style>

然后我将此主题应用于清单

中的整个应用程序
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/CustomActionBarTheme"  >

问题在于,条形图覆盖了需要保持可见的部分应用程序。我在应用程序中添加了以下内容并修复了第一个视图(您在其中键入消息并点击发送)

android:paddingTop="attr/actionBarSize"

但是,应该显示消息的以下活动仍由操作栏覆盖。我想知道在哪里添加顶部填充,以便我也可以修复此视图。 我做了一些搜索,发现这个'显示消息'活动的布局是在.java文件中定义的 (至少我认为是)

//Create the text view
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);

    //Set the text view as the activity layout
    setContentView(textView);

我尝试了一个textView.setPadding(),但该函数只将int作为参数,因此我无法传递类似“attr / actionBarSize”的内容

重申:需要帮助确定显示消息活动的布局在哪里以及我如何编辑它以便我可以像在主视图中那样设置Top padding。

谢谢!

2 个答案:

答案 0 :(得分:0)

您可以将ActionBar高度作为int:

int actionBarHeight = 0;

TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
    actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
}

您可以将该值用于setPadding()setMargins()

或者您可以将上边距/填充添加到第二个活动布局文件中,但如果不查看活动的代码或xml布局文件,则很难提供帮助。

答案 1 :(得分:0)

我在Android教程之后遇到了同样的问题。 Jones的上述答案解决了这个问题,但是这里有一种方法可以在布局xml文件中使用android:paddingTop="?attr/actionBarSize"来显示Display Message活动。

如果您按照教程Starting Another ActivityAdd Up Button for Low-level Activities进行操作,最终会得到/java/com.something.myfirstapp/DisplayMessageActivity.java,如:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_displaymessage);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    // Get the message from the intent
    Intent intent = getIntent();
    String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);

    // Create the text view
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);

    // Set the text view as the activity layout
    setContentView(textView);
}

上面的代码创建了一个超出此活动布局文件范围的新TextView。相反,你想要做的是重用已经在/res/layout/activity_display_message.xml文件中声明的“Hello World”TextView:

<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"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="my.vstore.myfirstapp.DisplayMessageActivity">

<TextView android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</RelativeLayout>

在TextView android:id="@+id/display_message"中添加ID,然后将paddingTop更改为android:paddingTop="?attr/actionBarSize"

<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"
    android:paddingTop="?attr/actionBarSize"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="my.vstore.myfirstapp.DisplayMessageActivity">

<TextView android:id="@+id/display_message"
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</RelativeLayout>

然后,找到TextView的id并在/java/com.something.myfirstapp/DisplayMessageActivity.java中设置文本:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_displaymessage);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    // Get the message from the intent
    Intent intent = getIntent();
    String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);

    // Find the text view
    TextView textView = (TextView) findViewById(R.id.display_message);
    textView.setTextSize(40);
    textView.setText(message);
}

因此,您可以在布局xml文件中使用android:paddingTop="?attr/actionBarSize"来显示“显示消息”活动。