活动栏中未显示图标

时间:2015-09-11 18:13:00

标签: android

我在操作栏上显示图标时遇到问题。它显示我在溢出菜单中设置的文本/标题(三个点),但没有动作。这是我的代码,我错过了什么?

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_vehicle_table);
    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.vehicle_table, new VehicleTable())
                .commit();
    }
     getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);  
    getSupportActionBar().setCustomView(R.layout.custom_vehicle_action_bar_layout);
}

VehicleTable.java(我的片段)

public class VehicleTable extends Fragment {

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // Inflate the menu; this adds items to the action bar if it is present.
    inflater.inflate(R.menu.menu_vehicle_table, menu);
}

menu_vehicle_table.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  tools:context="com.roadstruck.mobile.VehicleTable">


<item android:id="@+id/add_vehicle"
    android:icon="@drawable/ic_add_white_24dp"
    android:orderInCategory="100"
    android:title="Add"
    android:showAsAction="always"
    />
</menu>

custom_vehicle_action_bar_layout(自定义文本视图,用于居中菜单文字)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="VEHICLES"
    android:textColor="#ffffff"
    android:id="@+id/mytext"
    android:textSize="18sp" />
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

尝试将android:showAsAction="always"更改为app:showAsAction="always"