简体   繁体   English

如何在列表视图下方添加文本视图?

[英]How to add a textview below a listview?

I am tying to have a layout where I have a listview at the top of my screen, textview which is just below the listview and a button at the bottom of the screen. 我想拥有一种布局,其中在屏幕顶部具有一个列表视图,在列表视图下方具有一个文本视图,在屏幕底部具有一个按钮。

This is my main.xml 这是我的main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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="0dp"
        android:layout_weight="1">
    </ListView>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:text="HELLO WORLD THIS IS A DAMN TEXTVIEW"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="HELLO WORLD"/>

</LinearLayout>
</RelativeLayout>

This is my MainActivity.java 这是我的MainActivity.java

package com.example.suudupa.customtextviews;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

    String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry","Android","IPhone","WindowsMobile","Blackberry","Android","IPhone","WindowsMobile","Blackberry",
            "WebOS","Ubuntu","Windows7","Max OS X"};
//    String[] mobileArray = {"Windows7","Max OS X"};

    @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);
    }
}

And activity_listview.xml 和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="10dp"
    android:textSize="16sp"
    android:textStyle="bold" >
</TextView>

When I have a large number of items in my listview, this works perfectly however when I have a few items, the textview sticks to the top of button rather than sticking to bottom of listview. 当我的列表视图中有大量项目时,这非常理想,但是当我的项目很少时,textview会停留在按钮的顶部而不是底部。

Please help me out. 请帮帮我。

You can try to add another layout over your listview to keep listview full height. 您可以尝试在列表视图上添加其他布局,以使列表视图保持完整高度。

Something like this: 像这样:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical">
<ListView
    android:id="@+id/mobile_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
</ListView>
</LinearLayout>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="HELLO WORLD THIS IS A DAMN TEXTVIEW"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="HELLO WORLD"/>

Try to use ConstraintLayout like below example 尝试使用ConstraintLayout,如下面的示例

    <ListView
        android:id="@+id/mobile_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/txtTest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:paddingBottom="50dp"
        android:text="HELLO WORLD THIS IS A DAMN TEXTVIEW"
        app:layout_constraintTop_toBottomOf="@+id/mobile_list" />

    <Button
        android:id="@+id/btnText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="HELLO WORLD"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />


</android.support.constraint.ConstraintLayout>

This work fine for me: 这项工作对我来说很好:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  <ListView
      android:id="@+id/mobile_list"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      >
  </ListView>
  <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="HELLO WORLD THIS IS A DAMN TEXTVIEW"
      android:layout_below="@+id/mobile_list"
      />
  <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="HELLO WORLD"
      />

</RelativeLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM