问题描述
我在教程的帮助下构建了一个Android应用。
这是MainActivity.java:-
package com.example.listdisplay;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
// Array of strings...
String[] mobileArray = {"Android","IPhone","WindowsMobile","BlackBerry","WebOS","Ubuntu","Windows7","Max OS X","PHP","Java","HTML","CSS","JavaScript","MysqL"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter adapter = new ArrayAdapter<String>(this,R.layout.activity_listview,mobileArray);
ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setAdapter(adapter);
}
}
这是activity_main.xml:-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/mobile_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
这是activity_listview.xml:-
<?xml version="1.0" encoding="utf-8"?>
<!-- Single List Item Design -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="20dip"
android:textSize="16dip"
android:textStyle="bold"
android:orientation="horizontal">
</TextView>
当我将textview更改为cardview等更多其他视图时。该应用程序将崩溃。 我想对Cardview和布局进行更多的样式设计。就像我也尝试了linearlayout.I插入此textview带有线性布局中的按钮。但是,它仍然会崩溃。
请帮助我。
解决方法
您无法将TextView
的{{1}}更改为activity_listview.xml
,因为ArrayAdapter
的构造函数期望布局只有EditText
。
如果要对其进行修改,则必须实现自定义ArrayAdapter
,以创建一个从其扩展的类。
您可以按照下一个中级教程来实现。 https://medium.com/mindorks/custom-array-adapters-made-easy-b6c4930560dd
如果要添加Button
或其他内容,则需要自定义ArrayAdapter
。
如果您想对卡片视图进行更多的样式设置,而无需将文本视图更改为卡片视图,只需简单地像下面的代码一样编写代码即可。
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:cardCornerRadius="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/label"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dip"
android:textSize="16dip"
android:textStyle="bold"
android:orientation="horizontal">
</TextView>
</LinearLayout>
</androidx.cardview.widget.CardView>
,
创建您的自定义xml文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="This is just text" />
</LinearLayout>
</androidx.cardview.widget.CardView>
重要部分是android:id="@+id/label"
中的TextView
。
然后将R.id.label
添加到ArrayAdapter
的第三个参数中,如下所示:
ArrayAdapter adapter = new ArrayAdapter<String>(this,R.layout.activity_listview,R.id.label,mobileArray);