0

我正在測試每個選項卡製作具有不同內容的選項卡式佈局。 在這個選項卡中,我希望使用Listview中的圖像和文本進行佈局。 這是我寫到目前爲止,但不工作的代碼。如何在片段佈局的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; 
    } 
} 

我新的這一點,任何人都可以幫我嗎?謝謝

回答

1

列表視圖使用適配器和編輯它具有圖像和文本定位,只要你想適配器

+0

如果你已經得到了你回答標記爲已回答 –

0

您需要進行自定義佈局(佈局1)佈局。然後製作一個自定義列表適配器(mAdapter),在你的mAdapter中使用layout1並填充數據。然後將mAdapter設置爲列表。

請看看這些教程,他們會幫助你明白這一切的自定義列表是如何工作的:

  1. Vogella List Tutorial

  2. Journal Dev List Tutorial