我想更改我到目前为止创建的应用程序,以实现ListVIEw.我跟着this example和this 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
是一个实现简单ListVIEw的“便利”类.这就是你所能做的一切.所以,那里没有setSupportActionbar(工具栏).
要达到您的要求,请使用AppCompatActivity
或默认Activity
.
This link提供了一个很好的教程,介绍如何使用新的Toolbar而不是旧的Actionbar.
你想要这样的东西:
可以使用此布局示例:
P.S.:“@ color / primary”可以是#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... }}
编辑:This other answer我可以向您展示如何创建自定义适配器,以在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,你很高兴.
总结以上是内存溢出为你收集整理的android – 如何在List活动中设置工具栏?全部内容,希望文章能够帮你解决android – 如何在List活动中设置工具栏?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)