【问题标题】:Overlaying Action Bar - Android Studio Tutorial叠加操作栏 - Android Studio 教程
【发布时间】:2015-05-31 11:02:13
【问题描述】:

在使用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() 但该函数只接受整数作为参数,所以我不能传递诸如“attr/actionBarSize”之类的东西

重申:需要帮助确定显示消息活动的布局在哪里以及如何编辑它,以便我可以设置顶部填充,就像我为主视图所做的那样。

谢谢!

【问题讨论】:

    标签: android android-actionbar overlay


    【解决方案1】:

    您可以将 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 布局文件就很难提供帮助。

    【讨论】:

    • 我在哪里定义这个整数以及如何将它传递给第二个活动的类?所以我感到困惑的原因是,在教程中,(developer.android.com/training/basics/firstapp/…) 他们要求你为第二个活动创建一个名为 activity_display_message 的布局,然后使用 textView 作为布局。这就是我没有将第二个布局的 xml 放在我的问题中的原因,因为它是默认的“Hello World!”文件。
    • 直接在需要使用的activity中定义。附:通常你为每个活动创建一个类和一个布局,你将它们与setContentView(R.layout.activity_display_message)结合起来,然后在java代码中更改布局的组件。所以,只需为第二个活动创建一个布局,在需要的地方使用android:paddingTop="attr/actionBarSize",就完成了。
    • 把你的代码放在 onCreate() 函数中,它可以完美运行,非常感谢。也感谢您的提示;只是我在按照教程进行操作,我认为他们使用 TextView 小部件进行布局。
    【解决方案2】:

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

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

    @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添加一个idandroid:id="@+id/display_message",然后把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中设置Text:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-04
      • 1970-01-01
      • 2013-10-30
      • 2021-02-26
      • 1970-01-01
      • 2016-07-20
      • 1970-01-01
      相关资源
      最近更新 更多