栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Android中使用自定义按钮实现自定义操作栏?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Android中使用自定义按钮实现自定义操作栏?

如果要使用ActionBarAPI,这几乎与你将获得的接近。我不确定你是否可以在ActionBar不进行奇怪的Window黑客操作的情况下在其上方放置一个色带,这是不值得的。至于更改MenuItems流程,你可以通过样式使这些内容更紧密。可能会是这样,但我尚未对其进行测试。

<style name="MyTheme" parent="android:Theme.Holo.Light">    <item name="actionButtonStyle">@style/MyActionButtonStyle</item></style><style name="MyActionButtonStyle" parent="Widget.ActionButton">    <item name="android:minWidth">28dip</item></style>

这是如何向你的中添加自定义布局并将其添加到的方法ActionBar。

    // Inflate your custom layout    final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate( R.layout.action_bar, null);    // Set up your ActionBar    final ActionBar actionBar = getActionBar();    actionBar.setDisplayShowHomeEnabled(false);    actionBar.setDisplayShowTitleEnabled(false);    actionBar.setDisplayShowCustomEnabled(true);    actionBar.setCustomView(actionBarLayout);    // You customization    final int actionBarColor = getResources().getColor(R.color.action_bar);    actionBar.setBackgroundDrawable(new ColorDrawable(actionBarColor));    final Button actionBarTitle = (Button) findViewById(R.id.action_bar_title);    actionBarTitle.setText("Index(2)");    final Button actionBarSent = (Button) findViewById(R.id.action_bar_sent);    actionBarSent.setText("Sent");    final Button actionBarStaff = (Button) findViewById(R.id.action_bar_staff);    actionBarStaff.setText("Staff");    final Button actionBarLocations = (Button) findViewById(R.id.action_bar_locations);    actionBarLocations.setText("HIPPA Locations");

这是自定义布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:enabled="false"    android:orientation="horizontal"    android:paddingEnd="8dip" >    <Button        android:id="@+id/action_bar_title"         />    <Button        android:id="@+id/action_bar_sent"         />    <Button        android:id="@+id/action_bar_staff"         />    <Button        android:id="@+id/action_bar_locations"         /></LinearLayout>

这是色带布局:要使用它,只需使用merge你在其中充气的任何布局setContentView。

<frameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="@dimen/colorstrip"    android:background="@android:color/holo_blue_dark" />

以下是Button样式:

<style name="ActionBarButton">    <item name="android:layout_width">wrap_content</item>    <item name="android:layout_height">wrap_content</item>    <item name="android:background">@null</item>    <item name="android:ellipsize">end</item>    <item name="android:singleLine">true</item>    <item name="android:textSize">@dimen/text_size_small</item></style><style name="ActionBarButtonWhite" parent="@style/ActionBarButton">    <item name="android:textColor">@color/white</item></style><style name="ActionBarButtonOffWhite" parent="@style/ActionBarButton">    <item name="android:textColor">@color/off_white</item></style>

这是我使用的颜色和尺寸:

<color name="action_bar">#ff0d0d0d</color><color name="white">#ffffffff</color><color name="off_white">#99ffffff</color><!-- Text sizes --><dimen name="text_size_small">14.0sp</dimen><dimen name="text_size_medium">16.0sp</dimen><!-- ActionBar color strip --><dimen name="colorstrip">5dp</dimen>

如果你想对它进行更多自定义,则可以考虑完全不使用ActionBar,但我不建议这样做。你也可以考虑阅读《Android设计指南》,以更好地了解如何设计自己的ActionBar.

如果你选择放弃ActionBar并使用自己的布局,那么Toasts当用户长按“ MenuItems”时,应确保添加可操作的内容。使用此Gist可以轻松实现。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/367673.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号