【问题标题】:Toolbar with Custom View not Displaying with Full Width in android带有自定义视图的工具栏在android中不以全宽显示
【发布时间】:2015-12-14 22:44:45
【问题描述】:

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

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

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

我的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开发新手,请帮助我!提前致谢

更新我的代码后,我的应用看起来像:-

【问题讨论】:

  • 您想在标签更改时更新工具栏吗?
  • 不不,我只想让绿线占据整个屏幕宽度。
  • 设置工具栏高度为 android:layout_height="?attr/actionBarSize"
  • 我试过但没有成功。

标签: android android-toolbar


【解决方案1】:

在工具栏上使用AppBar布局,见完整教程here

【讨论】:

  • 所以设计自定义ActionBar,使用ImageView而不是setLogo,记住工具栏内部使用框架布局来处理位置等。
  • 但是我如何使用菜单项。
  • ImageButton 具有可见操作和隐藏项目的自定义对话框
  • 你能举例说明吗?
【解决方案2】:

试试这个对我有用

    <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>

【讨论】:

  • 它还提供了菜单图标大小的间距。
【解决方案3】:

检查下面的代码是否有效。我在 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>

工具栏.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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 2018-01-01
    相关资源
    最近更新 更多