【问题标题】:Android - Adding buttons to the titleAndroid - 向标题添加按钮
【发布时间】:2013-05-28 12:26:46
【问题描述】:

请告诉我,如何向标题栏添加按钮。

  • 完全删除标题并使用 ImageView 创建自定义标题。

  • 使用其他方法

My Goal 类似于 Facebook 应用

更新: API 级别 >= 10

【问题讨论】:

  • 我通常会自己做标题栏,这样我就可以添加按钮、状态图标...需要自己编写ViewGroup。
  • 感谢您的回复...我想知道这是否是最佳实践方法?
  • 这个链接可以帮到你:stackoverflow.com/questions/5663068/…

标签: android facebook user-interface themes android-titlebar


【解决方案1】:

我认为,使用 Android 的 Actionbar 是最佳做法。 更喜欢操作菜单而不是自定义标题栏中的按钮。

请看:

关于actionbar的进一步实现请参考:

  1. http://www.vogella.com/articles/AndroidActionBar/article.html
  2. developer.android.com/guide/topics/ui/actionbar.html‎

【讨论】:

  • 感谢您的回复,我已经更新了我的问题... API 级别 10 是我的最低版本。
  • 是的..!可以选择从 API 级别 8 提供支持,您可以使用 SherlockActionbar 库。请从以下位置下载库:actionbarsherlock.com
【解决方案2】:

我的解决方案使用自定义 ViewGroup(来自 RelativeLayout),因此我可以在不同的活动中使用它,而无需复制粘贴 xml。这是一个使用标题更新文本视图的简单示例。您可以将按钮添加到相对布局中并让它们以您需要的方式工作。

但如果您只有一个 Activity,最好将其作为 Activity 主布局的子布局包含在内

public class StatusBar extends RelativeLayout {

private TextView textViewTitle;

    public StatusBar(Context context, AttributeSet attrs) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layoutInflater.inflate(R.layout.ui_status_bar, this, true);
        super(context, attrs);
}

@Override
    public void onFinishInflate() {
        super.onFinishInflate();

        textViewTitle = (TextView) findViewById(R.id.textViewTitle);
}

private void updateStatusBar(String title) {
this.textViewTitle.setText(title)
}
}

还有 ui_status_bar.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:background="@color/theme_color_darker"
    android:gravity="center_vertical" >

    <TextView
        android:id="@+id/textViewTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold" />

</RelativeLayout>

在您的活动中,您可以像这样使用它:

<com.uicomponents.statusbar.StatusBar
    android:id="@+id/statusBar"
    style="@style/status_bar" />

【讨论】:

  • 感谢您的回复。只是说清楚:R.layout.ui_status_bar 是对XML的引用吗?你能详细说明一下吗?
  • 如果您没有使用 custm View 和 viewGroups 的经验,这很难解释。 layout.ui_status_bar.xml 进入 Android 项目的 layout/ 文件夹。编译时,可以通过资源(即 R.layout.ui_status_bar)的引用来加载该布局,就像您为活动加载布局一样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-16
  • 2011-01-02
相关资源
最近更新 更多