【问题标题】:Toolbar in non-AppCompat project not inflating非 AppCompat 项目中的工具栏未膨胀
【发布时间】:2017-08-14 23:27:25
【问题描述】:

我尝试使用我的活动来实现我的工具栏,但它没有膨胀并且一直显示错误。我的项目没有使用 AppCompat,但我不确定这是否也是错误的原因。

扩展类 android.widget.Toolbar 时出错

Java

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar customToolbar = findViewById(R.id.toolbar_1line);
        setActionBar(customToolbar);

        //add back arrow to toolbar
        if (getActionBar() != null){
            getActionBar().setDisplayHomeAsUpEnabled(true);
            getActionBar().setDisplayShowHomeEnabled(true);
        }

        TextView mTitle = this.findViewById(R.id.toolbar_title);
        mTitle.setText(getString(R.string.select_a_destination_station));
        mTitle.setTextColor(Color.WHITE);
        mTitle.setEllipsize(TextUtils.TruncateAt.MARQUEE);
        mTitle.setMarqueeRepeatLimit(-1);
        mTitle.setSingleLine(true);
        mTitle.setSelected(true);
    }
}

工具栏布局

<?xml version="1.0" encoding="utf-8"?>
<android.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar_1line"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/actionBarSize"
    android:minHeight="?android:attr/actionBarSize">

    <LinearLayout
        android:id="@+id/singleline_text_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="vertical">

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@android:style/TextAppearance.Material.Widget.ActionBar.Title"/>
    </LinearLayout>
</android.widget.Toolbar>

activity_main.xml

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

    <include layout="@layout/toolbar_singleline"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <ListView
        android:id="@+id/list_objects"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

【问题讨论】:

    标签: android xml android-linearlayout android-toolbar


    【解决方案1】:

    试试这个。

    在 Toolbar_layout 中

    一种方式

    改变

    <android.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar_1line"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:minHeight="?android:attr/actionBarSize">
    </android.widget.Toolbar>
    

    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar_1line"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:minHeight="?android:attr/actionBarSize">
    </android.support.v7.widget.Toolbar>
    

    在java代码中

    改变

    Toolbar customToolbar = findViewById(R.id.toolbar_1line);
    setActionBar(customToolbar);
    
    //add back arrow to toolbar
    if (getActionBar() != null){
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setDisplayShowHomeEnabled(true);
    }
    

    Toolbar customToolbar = (Toolbar) findViewById(R.id.toolbar_1line);
    setSupportActionBar(customToolbar);
    //add back arrow to toolbar
    if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }
    

    另一种方式

    改变

    <android.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar_1line"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:minHeight="?android:attr/actionBarSize">
    </android.widget.Toolbar>
    

    <Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar_1line"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:minHeight="?android:attr/actionBarSize">
    </Toolbar>
    

    这很容易。

    【讨论】:

      猜你喜欢
      • 2015-03-10
      • 2016-03-21
      • 1970-01-01
      • 1970-01-01
      • 2021-09-14
      • 2019-07-01
      • 2022-01-10
      • 2021-06-21
      • 1970-01-01
      相关资源
      最近更新 更多