【问题标题】:Android ICS to JB Actionbar tabs changed requirementsAndroid ICS 到 JB Actionbar 选项卡的更改要求
【发布时间】:2013-01-17 11:45:20
【问题描述】:

我有一个针对 SDK 15、ICS 4.0.3 的应用程序。我刚刚将它更新为指向 4.1,它破坏了我的用户界面,让它变得非常丑陋:

以下是 API 15 上通常的样子:

这是 16 或 17 日:

我感觉这是因为 Nexus 7 使用手机 UI 而不是实际的平板电脑 UI...有没有办法强制标签进入操作栏而无需使用 ActionBar sherlock 之类的东西或其他东西(比如子类化)?或者即使我可以让标签看起来不那么糟糕,也许如果它们有居中的文本?现在按钮甚至不做 match_parent。定位较低的 SDK 是否有缺点(我现在不需要更高的 SDK)?我的应用只能在 ICS 或更高版本上运行。

【问题讨论】:

    标签: android tabs android-actionbar android-4.0-ice-cream-sandwich android-4.2-jelly-bean


    【解决方案1】:

    我创建了类似 instagram 的操作栏类似标签,您可以查看我之前的答案以及我是如何做到的。我正在使用手机,我无法在我的笔记本电脑上发布代码。无论如何,只需检查我以前的答案。稍后我将再次访问并发布代码。

    编辑------

    Edit here is the image :). 
    

    再次以最佳结果返回 :)。你需要做的第一件事是 创建一个布局,命名为 header.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="@dimen/action_bar_height"
        android:layout_gravity="top"
        android:baselineAligned="true"
        android:orientation="horizontal" >
    
        <ImageView
            android:id="@+id/share"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:layout_weight=".14"
            android:background="@drawable/action_bar_left_button"
            android:src="@drawable/action_bar_glyph_back" />
    
        <ImageView
            android:id="@+id/bright"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".14"
            android:background="@drawable/action_bar_left_button"
            android:src="@drawable/action_bar_glyph_lux" />
    
        <ImageView
            android:id="@+id/rotate"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".14"
            android:background="@drawable/action_bar_left_button"
            android:src="@drawable/rotate" />
    
        <ImageView
            android:id="@+id/bright"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".14"
            android:background="@drawable/action_bar_left_button"
            android:src="@drawable/action_bar_glyph_lux" />
    
        <ImageView
            android:id="@+id/rotate"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".14"
            android:background="@drawable/action_bar_left_button"
            android:src="@drawable/rotate" />
    
        <ImageView
            android:id="@+id/forwa"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".14"
            android:background="@drawable/action_bar_left_button"
            android:src="@drawable/forward" />
    
    </LinearLayout>
    

    之后转到您的 MainActivity.class 并创建此方法。

        private void setupActionBar() {
        ActionBar actionBar = getActionBar();
        //actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);
        ViewGroup v = (ViewGroup)LayoutInflater.from(this)
            .inflate(R.layout.header, null);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
                ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBar.setCustomView(v,
                new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,
                        ActionBar.LayoutParams.WRAP_CONTENT,
                        Gravity.CENTER_VERTICAL | Gravity.RIGHT));
    
    } 
    

    setupActionBar(); 添加到您的 onCreate Activity 并运行您的应用:)。

    现在您有了带有分隔线和图像的自定义 ActionBar P.S 分隔线在布局中定义为图像背景:)。

    【讨论】:

    • 虽然这很好用(我将来可能会使用它),但它并不能完全解决标签移动到操作栏的问题(尽管它可以用来这样做) .我正在使用 viewpager,所以我可以向左/向右滑动来切换标签,它需要更多的工作才能让它工作,但如果我不能,我可能会制作一个自定义操作栏。
    • 您可以将 viewpage 与 this 一起使用,或者您可以使用 Horizo​​ntalScrollView 使它们滚动,或者您可以简单地向 viewpager 添加一个功能,当您滚动它时,它会使 imgeView 突出显示。假设你使用 imageView,所以当你从第一页到第二页之间滚动时,它会在我的示例中突出显示上面的图像。只需在您的视图页 onCreate 中添加此方法,因此当您滚动到它时,它会突出显示图像 imageView1.setImageResource(R.drawable.PressedImage)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多