第二个活动有一个ListView
,它不显示它。
UserList
中的Log.i
显示活动处于活动状态。
主要活动.java:
public void makeUserListIntent() {
Intent userListIntent = new
Intent(getApplicationContext(),UserList.class);
startActivity(userListIntent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if ( ParseUser.getCurrentUser() != null ) makeUserListIntent();
}
用户列表.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_list);
Log.i("Second Activity: ", "onCreate");
ListView listViewUserList = findViewById(R.id.listViewUserList);
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("Test User");
arrayList.add("Test User");
ArrayAdapter arrayAdapter = new
ArrayAdapter(this,android.R.layout.simple_list_item_1,arrayList);
listViewUserList.setAdapter(arrayAdapter);
listViewUserList.setVisibility(View.VISIBLE);
}
activiy_user_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".UserList">
<ListView
android:id="@+id/listViewUserList"
android:layout_width="395dp"
android:layout_height="715dp"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
我应该看到ListView
但出现空白页。
UserList.java
中的Log.i
显示活动被调用。尝试设置visibility
等。
为什么我看不到ListView
?
请像这样编辑activiy_user_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".UserList">
<ListView
android:id="@+id/listViewUserList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
这里的问题是您将高度设置为常量值715dp
,因此前几项在较小的设备上将不可见。要解决此问题,请将您的高度更改为match_parent
并在顶部添加一些边距(如果这是您想要的输出)