3 回答
TA贡献1843条经验 获得超7个赞
您应该为您的活动创建一个菜单:
里面res -> menu选择new -> Menu资源文件
在您的新菜单 xml 文件中写入以下内容:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_action_ok"
android:title="Ok"
app:showAsAction="ifRoom" />
</menu>
基本上,您正在创建一个菜单,其中包含一个名为“确定”的选项,但如果需要,您可以有更多选项。如果需要,您还可以自定义此选项的视图:
app:actionLayout="@layout/filter_ka"
在您的父活动中:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu.
getMenuInflater().inflate(R.menu.menu_test, menu);
return true;
}
R.菜单。menu_test是菜单文件的名称。
最后要接收菜单选项的点击,您应该重写以下函数:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.menu_action_ok) {
//Your code
return true;
}
return super.onOptionsItemSelected(item);
}
现在你应该有一个像这样的菜单:
TA贡献1811条经验 获得超5个赞
您必须为此创建一个菜单。假设你创建add_menu
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_add"
android:title="ADD"
app:showAsAction="ifRoom"/>
</menu>
创建菜单后,使用以下命令将其扩展到您的子活动。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.add_menu, menu);
return true;
}
TA贡献1794条经验 获得超8个赞
您不必使用操作栏。您只能使用您想要的背景颜色的视图。在您将按钮设置在彩色区域上方之后。
在 Android Studio 中:
价值观>风格
用这个 :
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
代替
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
回到你的布局:
// Change your background with LinearLayout's background propoerty
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#FFA908"
>
// You can put here whatever you want. İf you wish image or button.
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Example"
android:layout_gravity="center"
android:layout_marginLeft="200dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Example"
android:layout_gravity="center"
/>
</LinearLayout>
İf 你可以像这样使用。您不需要使用操作栏。你可以轻松地创造任何你想要的东西。
添加回答
举报