自定义操作栏不膨胀

时间:2015-03-04 02:03:29

标签: android android-layout android-actionbar android-xml

我试图制作自定义操作,但它无法正常工作。我按照this的说明几乎到了点,但它没有用。这是我的操作栏(action_bar_tablet.xml)的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="56dp"
android:background="@drawable/actionbar_background"
android:padding="16dp">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:text="The Commuter"
    android:textSize="24sp"
    android:textColor="#FFFFFF"/>
<Spinner
    android:layout_width="120dp"
    android:layout_height="24sp"
    android:entries="@string/lines"
    android:background="#FFFFFF"
    android:clickable="true"
    android:layout_marginLeft="80dp"/>
 </LinearLayout>

这是我用来充气的MainActivity方法:

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (findViewById(R.id.container) != null) {
        TABLET = false;

        //...

    } else {
        if (savedInstanceState == null) {
            TABLET = true;

            //...

            final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(
                    R.layout.action_bar_tablet,
                    null);
            final ActionBar actionBar = getActionBar();
            actionBar.setDisplayShowHomeEnabled(false);
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setDisplayShowCustomEnabled(true);
            actionBar.setCustomView(actionBarLayout);

        }
    }
}

这看起来很简单,但我不确定为什么它不起作用。有人可以帮帮我吗?

提前致谢:)

编辑1:

这里是string.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">The Commuter Chicago</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="lines">Lines</string>
<string name="stops">Stops</string>
<string name="arrivals">Arrivals</string>
<string-array name="lines">
    <item>Red</item>
    <item>Blue</item>
    <item>Brown</item>
    <item>Green</item>
    <item>Orange</item>
    <item>Purple</item>
    <item>Pink</item>
    <item>Yellow</item>
</string-array>

</resources>

2 个答案:

答案 0 :(得分:0)

如果ActivityActionBarActivity

,则需要尝试此操作
 @Override  protected void onCreate(Bundle savedInstanceState)   { 
super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_pager_and_easmain);

setContentView(R.layout.activity_main);

  getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

 getSupportActionBar().setCustomView(R.layout.action_bar_tablet);

并将您的代码更改为检查

的条件
if (findViewById(R.id.container) != null) {

不要检查它只是在setContent()方法之后初始化你的Actionbar。

答案 1 :(得分:0)

Dear change your code and fellow this.  
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar mActionBar = getActionBar();
        mActionBar.setDisplayShowHomeEnabled(false);
        mActionBar.setDisplayShowTitleEnabled(false);
        LayoutInflater mInflater = LayoutInflater.fr`enter code here`om(this);
        View mCustomView = mInflater.inflate(action_bar_tablet, null);
        TextView mTitleTextView = (TextView) <br>             mCustomView.findViewById(R.id.title_text);
        mTitleTextView.setText("My Own Title");

        ImageButton imageButton = (ImageButton) mCustomView
                .findViewById(R.id.imageButton);
        imageButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), "Refresh Clicked!",
                        Toast.LENGTH_LONG).show();
            }
        });

        mActionBar.setCustomView(mCustomView);
        mActionBar.setDisplayShowCustomEnabled(true);
    }
[enter link description here][1]


  [1]: http://javatechig.com/android/actionbar-with-custom-view-example-in-android