3 回答

TA貢獻(xiàn)1843條經(jīng)驗 獲得超7個贊
您應(yīng)該為您的活動創(chuàng)建一個菜單:
里面res -> menu選擇new -> Menu資源文件
在您的新菜單 xml 文件中寫入以下內(nèi)容:
<?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>
基本上,您正在創(chuàng)建一個菜單,其中包含一個名為“確定”的選項,但如果需要,您可以有更多選項。如果需要,您還可以自定義此選項的視圖:
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是菜單文件的名稱。
最后要接收菜單選項的點擊,您應(yīng)該重寫以下函數(shù):
@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);
}
現(xiàn)在你應(yīng)該有一個像這樣的菜單:

TA貢獻(xiàn)1811條經(jīng)驗 獲得超5個贊
您必須為此創(chuàng)建一個菜單。假設(shè)你創(chuàng)建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>
創(chuàng)建菜單后,使用以下命令將其擴(kuò)展到您的子活動。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.add_menu, menu);
return true;
}

TA貢獻(xiàn)1794條經(jīng)驗 獲得超8個贊
您不必使用操作欄。您只能使用您想要的背景顏色的視圖。在您將按鈕設(shè)置在彩色區(qū)域上方之后。
在 Android Studio 中:
價值觀>風(fēng)格
用這個 :
<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 你可以像這樣使用。您不需要使用操作欄。你可以輕松地創(chuàng)造任何你想要的東西。
添加回答
舉報