在向xml文件添加按钮时,将覆盖使用自定义适配器的ListView

时间:2018-01-05 05:08:29

标签: android xml listview

我正在尝试显示一个项目列表(每个条目包含3个文本视图),后面跟着一些按钮,这些按钮将切换到其他活动,其中存在类似的设置。虽然不涉及按钮时ListView完全填充,但只要我尝试添加按钮就会消失。我认为这可能与我将music_list.xml文件专门设置为列表有关,但从查看其他来源可能有一个ListView后跟其他视图。目前,文本按钮不起作用;在尝试编写任何逻辑之前,我试图让它显示ListView。

我的music_list.xml文件,其中有一个列表,希望按钮:

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

<ListView
    android:id="@+id/music_list"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_alignParentTop="true"
    android:layout_weight="1.0"/>


<LinearLayout
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:gravity="bottom|center_horizontal"
    android:layout_alignParentBottom="true"
    android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="24dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_weight="0"
        android:text="Test 1"
        android:layout_marginTop="32dp"
        />
</LinearLayout>

我的music_item文件,其中包含用于填充列表的3个TextView;

<?xml version="1.0" encoding="utf-8"?>
<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">


<TextView
    android:id="@+id/artist_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="16dp"
    android:layout_weight="1"
    android:gravity="left"
    tools:text="lutti" />

<TextView
    android:id="@+id/title_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="16dp"
    android:layout_weight="1"
    android:gravity="center"
    tools:text="one" />

<TextView
    android:id="@+id/album_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="16dp"
    android:layout_weight="1"
    android:gravity="right"
    tools:text="one" />

</LinearLayout>

活动页面的当前逻辑,它填充列表但尚未对其下面的按钮执行任何操作:

package com.example.android.musicapp;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;

import java.util.ArrayList;

/**
 * Created by ~ Adam ~ on 1/2/2018.
 */

public class MyMusicActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.music_list);


        ArrayList<Music> music = new ArrayList<Music>();

        music.add(new Music("The Who", "Won't Get Fooled Again", "Who's Next"));
        music.add(new Music("A Great Big World", "There's an Answer", "Is there Anybody Out There?"));
        music.add(new Music("James Blake", "Retrograde", "Overgrown"));
        music.add(new Music("Chopin", "Ballade No.3 in A Flat, Op.47", "Chopin: Ballades"));
        music.add(new Music("Kendrick Lamar", "Loyalty (Feat. Rihanna)", "DAMN."));
        music.add(new Music("Schel", "When the Dragon Came Down", "Schel"));
        music.add(new Music("Man man", "Loot My Body", "On Oni Pond"));
        music.add(new Music("Sarah Jarosz", "Dark Road", "Build Me Up From Bones"));
        music.add(new Music("Jimmy Buffett", "Come Monday", "Songs You Know By Heart"));

        MusicAdapter adapter = new MusicAdapter(this, music);
        ListView listView = (ListView) findViewById(R.id.music_list);
        listView.setAdapter(adapter);
    }
}

1 个答案:

答案 0 :(得分:0)

当您在第一个buttons中添加layout时,该布局会将您的listview layout推向左侧,这就是您无法看到list的原因。尝试使用relativeLayout或正确放置linearLayout。我使用了RelativeLayout,现在工作正常:

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

<ListView
    android:id="@+id/music_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true" />


<RelativeLayout
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:gravity="bottom|center_horizontal"
    android:layout_alignParentBottom="true"
    android:layout_height="wrap_content">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="24dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:text="Test 1"
        android:layout_marginTop="32dp"
        />
</RelativeLayout>