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
缺乏orientation
XML 上的参数,以及您的TextView
had a android:layout_height
set to match_parent
,所以它填满了整个布局。我已将其更改为wrap_content
现在就可以走了。