把我动作条在底部(Placing my ActionBar at the bottom)

2019-06-27 15:50发布

我跟在我的应用程序越来越为其他动作的教程。 但是,即使当我把android:uiOptions="splitActionBarWhenNarrow"在我的清单文件时,它仍然保持它在顶部。 任何人有什么用此代码做任何想法?

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:orientation="vertical"
android:background="#f0f0f0" 
android:baselineAligned="false">

<LinearLayout 
    android:id="@+id/myFragments"
    android:layout_width="match_parent"
    android:layout_height="0dp">

</LinearLayout>

</LinearLayout>

清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alotoftesting"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" 
        android:uiOptions="splitActionBarWhenNarrow">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

Answer 1:

谷歌的默认操作栏不会在任何情况下在底部出现。 因为它似乎其他人所说, splitActionBarWhenNarrow只有把你的一个子集ActionBar时,该设备是窄(如标签等),在屏幕的底部。 不幸的是,如果你想实现像在屏幕的底部接口的动作条,你就必须实现它自己( 一个快速搜索发现这个例子 ),因为我还没有看到任何图书馆为你做到这一点。

总之,虽然,我不推荐它,因为它违反了无缝设计原则通过打破其中的ActionBar类型控件将是用户期望在设计文件-一个Android用户非常习惯做的事情一定的方式,你会需要一个非常令人信服的理由打破这种预期。



Answer 2:

根据此评论:

多少菜单项,你在你的动作条有哪些? 该splitActionBarWhenNarrow选项基本上允许溢出到第二,“分裂”的行动上,如果你的菜单项将不适合在顶部底部栏。 如果你所有的菜单项适合在顶部,你将看不到分开布置。

如果你想有一个自定义的底部工具栏,请我回答这个问题(下面加):

创建自定义工具栏底部

我已经创建了一个简单的应用程序应该表现出你如何开始

创建自定义的ViewGroup

这是我的activity_main.xml布局文件:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="0dp" tools:context="com.example.piotr.myapplication.MainActivity"> <LinearLayout android:id="@+id/show_pdf" android:layout_width="match_parent" android:layout_height="40dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:background="@color/primary_material_light" android:orientation="horizontal"> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:src="@drawable/abc_ic_menu_cut_mtrl_alpha"/> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:src="@drawable/abc_ic_menu_copy_mtrl_am_alpha"/> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:src="@drawable/abc_ic_menu_selectall_mtrl_alpha"/> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:src="@drawable/abc_ic_menu_paste_mtrl_am_alpha"/> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:src="@drawable/abc_ic_menu_share_mtrl_alpha"/> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:src="@drawable/abc_ic_menu_selectall_mtrl_alpha"/> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:src="@drawable/abc_ic_menu_moreoverflow_mtrl_alpha"/> </LinearLayout> <EditText android:id="@+id/editText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="75dp" android:ems="10" android:inputType="textPersonName" android:text="Name"/> </RelativeLayout> 

正如你可以看到我的父ViewGroupRelativeLayout ,它只是允许我创建在屏幕底部的视图。

请注意,我设置布局边距为零(我认为:在设置布局保证金这里零是没有必要的,效果一样)。 如果你想改变它,工具栏将不会使用全宽,它不会与屏幕下方贴。

然后我添加了一个线性布局与硬编码高度为:

  android:layout_height="40dp" 

我想它,所以我把它作为我的底部工具栏将充分可用宽度match_parent

接下来,我添加了一些ImageButton看法与Android的库的图像。

有你有两种可能性:

  • 如果你真的想拥有像上面的例子工具栏只是删除每一个ImageButton观点这一行:

      android:layout_weight="1" 

除去重量和一些按钮后,你会得到非常相似预期的看法:

  • 如果你想利用全宽,使每个按钮在你的项目中同样大小的使用weight在这个矿的例子。

现在让我们去我的AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?> <manifest package="com.example.piotr.myapplication" xmlns:android="http://schemas.android.com/apk/res/android"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:windowSoftInputMode="stateVisible|adjustResize"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest> 

在该文件中我已经加你只能看到一个额外一行:

  android:windowSoftInputMode="stateVisible|adjustResize"> 

以确保设备的键盘不会隐藏我的自定义工具栏底部。

来源: 如何底部菜单添加到Android活动

我这么想,你也可以把标签底部如果需要的话。

如果您有任何疑问,请免问。

希望它能帮助



文章来源: Placing my ActionBar at the bottom