如何创建自己的标题栏?

时间:2015-11-28 02:54:47

标签: android android-layout

我正在尝试构建自己的标题栏。现在,我已经制作了我的xml代码,但我不知道如何更换新代码。这两个代码都粘贴在下面:

mytitlebar.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="@string/titlebar_height"
    android:background="@color/colorPrimary"
    android:id="@+id/customTitleBar">

    <ImageView
        android:id="@+id/logoTitleBar"
        android:layout_weight="7"
        android:scaleType="fitStart"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:layout_width="0dp"
        android:src="@mipmap/white_launcher"
        />

    <Button
        android:layout_weight="1"
        android:id="@+id/tbMessageButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/titlebar_message_button"
        android:layout_gravity="center|center_horizontal"
        />

</LinearLayout>

mymainactivity.java

public class MainScreenActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

  @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_screen);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }
}

1 个答案:

答案 0 :(得分:0)

将onCreate方法设为:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_screen);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitlebar.xml);
}