【问题标题】:ListView using custom Adapter is overwritten when also adding buttons to the xml file将按钮添加到 xml 文件时,使用自定义适配器的 ListView 被覆盖
【发布时间】:2018-01-05 05:08:28
【问题描述】:

我试图显示一个项目列表(每个条目包含 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);
    }
}

【问题讨论】:

  • 将权重属性应用于水平线性布局而不是按钮
  • 您正在尝试在您拥有 listview 的第一个布局中添加按钮?对。

标签: android xml listview


【解决方案1】:

当您在第一个 layout 中添加 buttons 时,该布局会将您的 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>

【讨论】:

  • 感谢您的帮助!看起来我误解了 wrap_content 和 match_parent 标签以及 layout_weight。它现在正在工作。
  • @AdamRubin 很高兴我能帮上忙。是的,主要问题是您给出了错误的 weights 和 match_parent 作为 width 。快乐编码:)
猜你喜欢
  • 2017-10-15
  • 2017-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多