【问题标题】:Simple way to center app icon in actionbar在操作栏中将应用程序图标居中的简单方法
【发布时间】:2014-05-23 22:18:27
【问题描述】:

我希望我的应用程序图标出现在操作栏的中心 - 根本不需要对操作栏进行其他更改。

我查看了更易于理解的解决方案,例如这个: ActionBar logo centered and Action items on sides

虽然这是可能的,但对于这样一个简单的用例来说似乎有点过分了。有没有什么办法可以通过简单的造型来实现?我使用的是默认的 Holo Light 主题,带有基本 API 11。

【问题讨论】:

  • 糟糕,刚刚点击了您的链接。它确实看起来有点矫枉过正,有类似的问题,但最终还是以矫枉过正的方式做

标签: android android-actionbar


【解决方案1】:

为什么不为你的 ActionBar 设置一个背景,其中包含图像(就像一个条带用于操作栏背景,你的图像位于其中心,九个补丁会是首选)。

然后您可以将其指定为操作栏背景,

 <style name="AppTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">

    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    <item name="android:actionBarStyle">@style/MyActionBar</item>        
</style>

和,

<!-- general styles for the action bar -->
<style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar">

    <!-- Support library compatibility -->        
    <item name="android:background">@drawable/actionbar</item>// your nine patch image
    <item name="android:icon">@android:color/transparent</item>       
</style>

然后您也可以隐藏标题。这不是一个 hack,但考虑到你不想调整 ActionBar 这会起作用。玩得开心:)

【讨论】:

  • 您先生是个天才。谢谢!
【解决方案2】:

你可以这样做:

ActionBar action = getActionBar();
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.action_menu, null);
action.setCustomView(view);

action_menu.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10sp" >

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:text="Left button" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="The logo" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:text="Right button" />

</RelativeLayout>

【讨论】:

  • 感谢您的回答。我知道我可以这样做(参见链接示例),但这正是我想要避免的那种解决方案——我只想对操作栏的外观做一个很小的改变,而不必实现自定义布局这样做。一个可接受的解决方案是修改标准主题。
  • 我也评论了你的问题。我不得不做类似的事情,但最终还是这样做了。我知道这样的麻烦
猜你喜欢
  • 2013-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-06
相关资源
最近更新 更多