【问题标题】:How to make Image and Text on Listview in Fragment Layout如何在片段布局中的 Listview 上制作图像和文本
【发布时间】:2017-10-15 10:52:31
【问题描述】:

我正在测试制作每个选项卡具有不同内容的选项卡式布局。 在此选项卡中,我想在 Listview 中使用图像和文本进行布局。 这是我到目前为止编写的代码,但无法正常工作。

Layout_One.xml

    <RelativeLayout 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"
    tools:context="com.example.kreuzell.projecttest.MainActivity$PlaceholderFragment">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/menuText"/>
</RelativeLayout>

Layout_One.java

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ImageView;

import java.util.List;

public class Layout_One extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.layout_one, container, false);

        String[] menuText = {
                "Text 1",
                "Text 2",
                "Text 3"
        };

        Integer[] menuImage = {
                R.drawable.image_1,
                R.drawable.image_2,
                R.drawable.image_3
        }

        ListView listView = (ListView) rootView.findViewById(R.id.menuText);
        ListView listView = (ListView) rootView.findViewById(R.id.menuImage);

        ArrayAdapter<String> listViewAdapter = new ArrayAdapter<String>(
                getActivity();
                android.R.layout.simple_list_item_1,
                menuText,
                menuImage
        )

        listView.setAdapter(listViewAdapter);

        return rootView;
    }
}

我是新手,有人可以帮助我吗?谢谢

【问题讨论】:

    标签: android android-layout listview android-fragments android-arrayadapter


    【解决方案1】:

    为列表视图使用适配器并编辑适配器的布局

    【讨论】:

      【解决方案2】:

      您需要创建一个自定义布局(layout1),让您的图像和文本可以根据需要进行定位。然后制作一个自定义列表适配器(mAdapter),在您的 mAdapter 中使用 layout1 并用数据填充它。然后将 mAdapter 设置为列表。

      请查看这些教程,它们将帮助您了解所有这些自定义列表的工作原理:

      1. Vogella List Tutorial

      2. Journal Dev List Tutorial

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-09-28
        相关资源
        最近更新 更多