如何在列表活动中设置工具栏?

2024-03-08

我想更改迄今为止创建的应用程序,以实现ListView。我跟着 and 这个例子 http://www.mkyong.com/android/android-listview-example/。这些示例单独有效,但不能与我必须对迄今为止的现有应用程序进行的更改一起使用。

我有一个avticity_main.xml定义如下:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity" >

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"/>

    </android.support.design.widget.AppBarLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/main_grid"

        android:useDefaultMargins="true"
        android:alignmentMode="alignBounds"
        android:columnOrderPreserved="false"

        android:columnCount="4"
        >

        <TextView
            android:text="MainTitle"
            android:textSize="32dip"
            android:layout_columnSpan="4"
            android:layout_gravity="center_horizontal"
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <ListView
            android:id="@+id/list"
            android:layout_height="wrap_content"
            android:layout_width="match_parent">
        </ListView>                            
    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

我的主要活动有以下代码:

public class MainActivity extends AppCompatActivity  {
    static final String[] MOBILE_OS =
        new String[] { "Android", "iOS", "WindowsMobile", "Blackberry"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        ListView list = (ListView) findViewById(R.id.list);
        list.setAdapter(new ListAdapter(this, MOBILE_OS));

代码编译良好,但没有显示列表。


ListActivity http://developer.android.com/reference/android/app/ListActivity.html是一个“方便”类来实现一个简单的ListView。这几乎就是你能做的一切。所以,没有setSupportActionBar(Toolbar) there.

为了实现你想要的,使用AppCompatActivity http://developer.android.com/reference/android/support/v7/app/AppCompatActivity.html或默认Activity http://developer.android.com/reference/android/app/Activity.html.

这个链接 http://androidessence.com/how-to-replace-the-android-actionbar-with-toolbar/提供了一个关于如何使用新功能的很好的教程ToolBar而不是旧的ActionBar.

你想要这样的东西:

可以使用此布局示例:

P.S.: "@color/primary"可以是任何颜色,例如#FF009688.

活动主文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:id="@+id/toolbar"
        android:background="@color/primary"
        android:elevation="4dp">

    </android.support.v7.widget.Toolbar>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar"
        android:id="@+id/lv"></ListView>

</RelativeLayout>

在 Java 方面:

你的活动.java

public class MainActivity extends AppCompatActivity {


    private Toolbar toolbar;
    private ListView lv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        lv = (ListView) findViewById(R.id.lv);
        setSupportActionBar(toolbar);

        final ActionBar actionBar = getSupportActionBar();

        if (actionBar != null) {
            actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        // Populate the ListView here...

    }

}

EDIT: 这个另一个答案 https://stackoverflow.com/questions/30462112/listview-updated-from-edittext/30462677#30462677我的可以向您展示如何创建自定义适配器来在ListView.

第二次编辑:使用此布局

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity" >

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"/>

    </android.support.design.widget.AppBarLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/main_grid"

        android:useDefaultMargins="true"
        android:alignmentMode="alignBounds"
        android:columnOrderPreserved="false"

        android:columnCount="4"
        android:orientation="vertical">

        <TextView
            android:text="MainTitle"
            android:textSize="32dip"
            android:layout_columnSpan="4"
            android:layout_gravity="center_horizontal"
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <ListView
            android:id="@+id/list"
            android:layout_height="wrap_content"
            android:layout_width="match_parent">
        </ListView>
    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

我做了什么?

Your LinearLayout缺乏orientationXML 上的参数,以及您的TextView had a android:layout_height set to match_parent,所以它填满了整个布局。我已将其更改为wrap_content现在就可以走了。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在列表活动中设置工具栏? 的相关文章

  • 我用 MediaRecorder 录制的文件无法播放

    我正在使用 MediaRecoder 录制声音 但录制完成后无法播放 我尝试使用Google Play Music ES Media Player 甚至将其上传到电脑并尝试使用Winamp打开它 没什么玩的了 AUDIO RECORDER
  • 如何解决这个 java.nio.BufferOverflowException 错误?

    当我尝试使用 ADT 在 Eclipse 中编译代码时 它向我显示了此错误 2013 12 10 17 55 51 Android SDK Warning when loading the SDK Warning Ignoring buil
  • 如何为整个 Android 应用程序设置默认字体系列

    我在我的应用程序中使用 Roboto 浅色字体 要设置字体 我必须添加android fontFamily sans serif light 到每一个视图 有没有办法将 Roboto 字体声明为整个应用程序的默认字体系列 我已经尝试过这样的
  • 如何从活动启动另一个应用程序(下载或预安装)?

    基本上 我想获取所有已安装应用程序的列表 并选择一个从活动中运行 我尝试过使用 Intents 进行 ACTION PICK 但这似乎遗漏了已下载的应用程序 并且其中有一堆垃圾 Thanks to get the list of apps
  • 是否可以禁止 EditText 中的第一个数字为“0”

    您好 我只是想知道是否可以禁止用户输入的第一个数字为 0
  • 选项 多个活动通用的菜单项

    安卓系统设计指南 http developer android com design patterns help html表示 帮助 应始终放置为溢出菜单的最后一项 它不应该出现在 ActionBar 中 而且 它应该出现在每个活动中 以便
  • Android 版 YouTube 频道订阅

    我在我的应用程序中使用 YouTube API 用户可以使用其 Google 帐户登录 我希望用户能够订阅某些 YouTube 频道 这里似乎有一些关于网络的东西 https developers google com youtube su
  • Android-无法解析符号 BaseObservable

    我正在尝试在 android 中实现数据绑定示例并使用可绑定变量创建 POJO 但我收到此错误 请帮忙 我正在关注这个教程http www vogella com tutorials AndroidDatabinding article h
  • 在发布我的应用程序之前在 play.google 上获取我的应用程序的链接

    我想使用 facebook api 分享我的应用程序的链接 play google 上的链接 但在将应用程序发布到市场之前我必须拥有它才能将其放入我的代码中 除了发布后立即更新我的应用程序之外 还有其他解决方案吗 用这个 https pla
  • android-透明RelativeLayout

    我想要制作一个具有可绘制渐变作为背景的活动 并将在其背景顶部显示 4 个面板 相对布局 现在我想让 4 个面板透明 例如 50 以便也可以看到渐变背景 我搜索了谷歌 但我发现只能通过活动而不是布局来做到这一点 如何做我想做的事 您可以创建一
  • Android 拒绝来电

    我想拒绝android中的传入 我从这些链接中看到了很多代码 Android 完全控制手机 信息亭模式 可能吗 如何 https stackoverflow com questions 7121508 android taking comp
  • 如何检测屏幕何时关闭?

    是否可以检测屏幕何时关闭并捕获它 我想在 Android 的全局设置中将计时器设置为 15 秒 我正在尝试查找一些如何捕获唤醒锁定模式的信息 只需注册您的应用程序即可执行操作Intent ACTION SCREEN OFF https de
  • android listactivity onCheckedChangeListener

    我正在开发一款应用程序 并且我有一个ListActivity 其选择模式设置为choice mode multiple 现在我想重写方法 当一项被调用时调用该方法选中 未选中 我发现onCheckChanged 方法仅针对RadioGrou
  • 在 WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER 处出现错误

    我正在通过以下方式创建我的第一个动态壁纸this http www vogella com articles AndroidLiveWallpaper article html教程 但我收到错误can not be resolved or
  • 旋转 LayerDrawable 中的单个 RotateDrawable

    我正在构建一个带有某种指南针的应用程序 并且我想使用 LayerDrawable 来绘制指南针并为其设置动画 LayerDrawable 由用于指南针背景的静态背景图像和用于旋转部分的 RotateDrawable 组成 这是我的可绘制资源
  • 如何在Android中将字体粗细设置为细、常规

    我有 3 个文本视图 我需要将它们的粗细设置为 轻 常规 和 压缩 有人可以帮助我如何在 Android 中实现这一目标吗 Use android textStyle on a TextView设置文本样式 例如bold italic或正常
  • 使用 greenDao 动态查询

    我需要验证一些条件来创建完整的查询 QueryBuilder qb getMyObjDao queryBuilder if 某些条件 qb where MyObjDao Properties Prop1 eq someValue elseq
  • 电子邮件发不出去,短信中的链接为 404

    我们刚刚完成将 Google AppInvites 集成到我们的应用程序中 我们注意到以下问题 电子邮件不会被发送 而发送的短信会带有一个链接 显示会出现 404 页面 仅当我设置 设置其他平台目标应用程序 对于 Android 和 iOS
  • 如何从下到上连续移动图像?

    我一直在研究这个例子http obviam net index php a very basic the game loop for android http obviam net index php a very basic the ga
  • 在没有 ActionBarSherlock 的情况下更改 SearchView 中的光标颜色

    我正在尝试更改 ICS 中 SearchView 小部件上闪烁光标的颜色 我已经尝试过以下方法 Adding

随机推荐