我试图显示一个项目列表(每个条目包含 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);
}
}
当您添加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>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句