最近开始研究Android Studio发现一个listView的坑,就是constraint布局是不能直接显示listView的,需要借助水平或垂直布局来显示,否则系统无法显示会直接闪退。
下面是解决后的案例代码:
xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<Button
android:id="@+id/button2_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.293"
app:layout_constraintStart_toEndOf="@+id/textView2_1"
app:layout_constraintTop_toTopOf="@+id/textView2_1" />
<TextView
android:id="@+id/textView2_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/guideline2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.292"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.506" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.15731874" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline2">
<ListView
android:id="@+id/listView2_1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
activity段:
package com.example.listviewtest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity2 extends AppCompatActivity implements View.OnClickListener {
private Button button1;
private TextView textView1;
private ListView listView1;
private String[] str1=new String[] {
"aaaaa","bbbb","cccc","ddddd",
"sdfsf","eeeee","ffff","sdfdsfsd",
"sdfsfdsf","qqqqq","sdfkskf","lllll"
};
private ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
button1=findViewById(R.id.button2_1);
textView1=findViewById(R.id.textView2_1);
listView1=findViewById(R.id.listView2_1);
getListViewData();
button1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int id=v.getId();
switch (id){
case R.id.button2_1:
finish();
break;
}
}
public void getListViewData(){
List<String> tt=new ArrayList<>();
for(int i=0;i<str1.length;i++) tt.add(str1[i]);
adapter=new ArrayAdapter(this, android.R.layout.simple_list_item_1,tt);
listView1.setAdapter(adapter);
}
}
实现效果如下:
希望有类似问题的老表能够有效解决。
?
?
|