叠加动作栏-Android Studio教程
在使用Android教程时遇到一个小问题 我创建了一个自定义操作栏主题,其中android:windowActionBarOverlay属性被设置为true叠加动作栏-Android Studio教程,android,android-actionbar,overlay,Android,Android Actionbar,Overlay,在使用Android教程时遇到一个小问题 我创建了一个自定义操作栏主题,其中android:windowActionBarOverlay属性被设置为true <!-- the theme applied to the application or activity --> <style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar"> <i
<!-- 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>
但是,以下本应显示消息的活动仍在操作栏中。我想知道我在哪里添加了顶部填充,这样我也可以修复这个视图。
我做了一些搜索,发现这个“显示消息”活动的布局是在.java文件中定义的
(至少我认为是这样)
我尝试过使用textView.setPadding()函数,但该函数只接受int作为参数,因此无法传递类似“attr/actionBarSize”的内容
重申:需要帮助确定显示消息活动的布局在哪里,以及如何编辑它,以便可以像设置主视图一样设置顶部填充
谢谢 您可以将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布局文件,就很难提供帮助。在Android教程之后,我遇到了完全相同的问题。琼斯的上述回答解决了这个问题,但这里有一种方法可以在布局xml文件中为显示消息活动使用
android:paddingTop=“?attr/actionBarSize”
如果您按照本教程进行操作,最终将得到/java/com.something.myfirstapp/displaymessactivity.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\u display\u message.xml
文件中声明的“Hello World”文本视图:
<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>
然后,找到文本视图的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”
我在哪里定义这个整数,如何将它传递给第二个活动的类?因此,我感到困惑的原因是,在教程中,()他们要求您为第二个名为“活动显示消息”的活动创建一个布局,但随后使用textView作为布局。这就是为什么我没有把第二个版面的xml放在我的问题中,因为它是一个默认的“Hello World!”文件。直接在需要使用它的活动中定义它。另外,通常为每个活动创建一个类和一个布局,将它们与setContentView(R.layout.activity\u display\u message)
组合,然后在java代码中更改布局的组件。因此,只需为第二个活动创建一个布局,在需要的地方使用android:paddingTop=“attr/actionBarSize”,就完成了。将您的代码放入onCreate()函数中,它就能完美工作,非常感谢。也谢谢你的提示;我只是在学习教程,我认为他们使用TextView小部件进行布局。
@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);
}
<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>
<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>
@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);
}