如何在列表活动中设置工具栏? [英] How to set a toolbar in a List Activity?
问题描述
我想更改到目前为止已创建的应用程序,以实现ListView
.我遵循了此示例和此示例.仅这些示例有效,但与我必须对现有的现有应用程序进行的更改无法一起使用.
I want to change the app I have created so far, in order to implement a ListView
. I followed this example and this example. The examples alone work, but not together with the changes I had to make to my so-far existing app.
我有一个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>
并且我的主要活动有以下代码:
and I have the following code for my main activity:
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));
代码可以正常编译,但是没有显示列表.
The code compiles fine, but no list is shown.
推荐答案
ListActivity
是用于实现简单ListView
的便利"类.这几乎就是您所能做的.因此,这里没有setSupportActionBar(Toolbar)
.
ListActivity
is a "convenience" class to implement a simple ListView
. And that's pretty much all you can do. So, there's no setSupportActionBar(Toolbar)
there.
要实现所需的功能,请使用 AppCompatActivity
或默认 Activity
.
To achieve what you want, use AppCompatActivity
or default Activity
.
此链接提供了一个不错的教程关于如何使用新的ToolBar
而不是旧的ActionBar
.
This link provides a nice tutorial on how to use the new ToolBar
instead of the old ActionBar
.
您想要这样的东西:
可以使用以下布局示例:
This layout example could be used:
PS::"@color/primary"
可以是任何颜色,例如#FF009688
.
P.S.: "@color/primary"
could be any color like #FF009688
.
activity_main.xml
<?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方面:
YourActivity.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...
}
}
编辑:另一个答案我的可以向您展示如何创建自定义适配器以在ListView
上显示所需的数据.
This other answer of mine can show you how to create a Custom Adapter to display the data you want on the 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>
我做了什么?
您的LinearLayout
在XML上缺少orientation
参数,并且您的TextView
的android:layout_height
设置为match_parent
,因此它填充了整个布局.我已将其更改为wrap_content
,您很好.
Your LinearLayout
was lacking the orientation
parameter on the XML, and your TextView
had a android:layout_height
set to match_parent
, so it was filling the entire layout. I've changed it to wrap_content
and you're good to go.
这篇关于如何在列表活动中设置工具栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!