繁体   English   中英

自定义视图的工具栏未在android中显示全宽度

[英]Toolbar with Custom View not Displaying with Full Width in android

我在这里找到了很多与工具栏相关的答案,但没有一个答案可以帮助我。

我想要实现的是拥有一个扩展的工具栏,它将显示一个徽标和菜单项。

我试图实现这一点的方式是通过工具栏。 首先,我创建工具栏并添加我的自定义视图。

我的toolbar.xml: -

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/dl_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp"
android:clipToPadding="false">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize">


    <View
        android:layout_width="fill_parent"
        android:layout_height="5dp"
        android:background="#358c44"
        android:layout_alignParentTop="true"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:layout_alignParentBottom="true"
        android:background="#358c44"/>

</RelativeLayout>

</android.support.v7.widget.Toolbar>

我的名字活动: -

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle(null);
     toolbar.setLogo(R.mipmap.ic_launcher);
   /* toolbar.setPadding(10,10,10,10);*/
    getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
}

我的应用程序显示如下: -

在此输入图像描述

我是android开发新手请帮帮我! 提前致谢

更新我的代码我的应用程序看起来像: -

在此输入图像描述

在工具栏上使用AppBar布局,请参阅此处的完整教程

试试这个它对我有用

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        // imageView for logo
        <ImageView
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:layout_gravity="center_vertical"
            android:scaleType="centerInside"
            android:src="@drawable/title" />

        // This is for search button on the right side
        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:padding="10dp"
            android:src="@drawable/button_image"
            android:scaleType="fitXY"
            android:background="@android:color/transparent"
            android:layout_gravity="center_vertical|end"
            android:layout_marginEnd="16dp" />

    </FrameLayout>

</android.support.v7.widget.Toolbar>

在你的活动中,只需将其设置为这样

Toolbar toolbar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(toolbar);

对于菜单在主活动中添加此项

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menuSettings:
            // Do some thing on setting press
            break;
        case R.id.menuAboutUs:
            // Do some thing on About us 
            break;

    }

    return super.onOptionsItemSelected(item);
}

对于菜单项列表,在资源目录下的菜单目录中添加menu_main.xml文件,并在此文件中添加项目

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/menuSettings"
        android:title="Settings" />

    <item
        android:id="@+id/menuAboutUs"
        android:title="About Us" />

</menu>

检查下面的代码是否有效。 我在src \\ main \\ res \\ layout文件夹中使用了toolbar.xml文件。

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:background="#2196F3"
        android:layout_height="58dp"
        android:id="@+id/relative">
        <include
        android:id="@+id/toolbarFav"
        layout="@layout/toolbar"
            android:layout_width="match_parent"
            android:layout_height="58dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"/>
    </RelativeLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/relative">
</LinearLayout>
</RelativeLayout>

toolbar.xml

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="58dp"
    android:background="#FFFFFF"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.v7.widget.Toolbar>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM