【问题标题】:Custom ActionBar Issues自定义 ActionBar 问题
【发布时间】:2017-11-19 11:07:59
【问题描述】:

我已经从我的应用程序中删除了默认操作栏,并使用工具栏从头开始添加了一个自定义操作栏。我在操作栏中放置了一个 imageView,但我面临两个问题:

1) imageView 没有响应,因此它不适用于所有设备。我能做些什么来实现这种响应能力? 2) 我的应用程序中还有一个 OptionsMenu,在 Toggle 旁边是我的应用程序的标题。因为我希望我的图像在 ActionBar 内居中,所以我不希望那里有标题。如何从 ActionBar 中删除它?

提前感谢您的帮助和时间!

【问题讨论】:

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


    【解决方案1】:

    问题 1

    如果您的图片没有响应。您必须放置不同大小的图像以支持高端设备。把想要的图片放到文件夹drawable/drawable-hdpi/drawable-mdpi....

    • /drawable-ldpi 适用于低密度屏幕。
    • /drawable-mdpi 适用于中等密度屏幕。
    • /drawable-hdpi 用于高分辨率屏幕。
    • /drawable-xhdpi 用于超高分辨率屏幕。

    问题 2:

    删除工具栏标题

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    toolbar.setTitle("");
    

    居中图像视图。将您的自定义视图编辑为居中。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"> 
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/your_icon"
        android:layout_gravity="center"/>
    </LinearLayout>
    

    【讨论】:

    • 谢谢你这工作正常,这是一个简单的修复! :D
    • 欢迎提供帮助!
    【解决方案2】:

    1) 不要使用 ImageView 的固定值在工具栏自定义布局中为 imageview 提供权重。 2) layout_gravity="center" 可能对你有帮助

    【讨论】:

    • 1) 对不起,但我不明白你对 ImageView 的重量和 2) layout_gravity 不会从自定义操作栏中删除应用程序的标题。我尝试了不同的方法,但没有奏效
    • 找到你的工具栏并在 onCreate() 中设置如下:toolbar = findViewById(your toolbar); setSupportActionBar(工具栏);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-18
    • 2013-03-11
    • 1970-01-01
    相关资源
    最近更新 更多