gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 13:23:08 25 4
gpt4 key购买 nike

我想实现自定义 ActionBar,它必须如下所示:

enter image description here

所以问题:

  1. 如何实现像自定义 View 这样的按钮:只是一些图像?
  2. 如何在 ActionBar 的顶部画一条线?
  3. 如何实现不带分隔线的按钮:在 ActionBar 上添加选项卡或什么?

最佳答案

enter image description here

如果您想使用 ActionBar API,这几乎与您将获得的一样接近。我不确定您是否可以在 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"
style="@style/ActionBarButtonWhite" />

<Button
android:id="@+id/action_bar_sent"
style="@style/ActionBarButtonOffWhite" />

<Button
android:id="@+id/action_bar_staff"
style="@style/ActionBarButtonOffWhite" />

<Button
android:id="@+id/action_bar_locations"
style="@style/ActionBarButtonOffWhite" />

</LinearLayout>

这是彩条布局:要使用它,只需在 setContentView 中添加的任何布局中使用 merge

<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 Design Guidelines更好地了解如何设计您的 ActionBar。

如果您选择放弃 ActionBar 并改用自己的布局,则应确保在用户长按您的“MenuItems”时添加可操作的 Toasts。这很容易实现using this Gist .

关于java - 如何在 Android 中使用自定义按钮实现自定义操作栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15518414/

25 4 0