Define list view with static elements on android in xml?

后端 未结 4 1928
傲寒
傲寒 2021-02-05 00:54

I want to create fragment which would display static menu as a set of rows in a list.

I like iOS method with static cells in a tableview. How can I achieve this in andro

相关标签:
4条回答
  • 2021-02-05 01:13

    Unfortunately, list view definition via xml is forbidden. Mess with adapters is required instead.

    0 讨论(0)
  • 2021-02-05 01:18

    You can use a vertical LinearLayout nested within a ScrollView to get what you're looking for. Put your "listview" items inside the LinearLayout, and style them to look like elements of a listview.

    If you absolutely must use a ListView (e.g. you're using ListFragment), you'll have to use a ListAdapter subclass or roll your own.

    0 讨论(0)
  • 2021-02-05 01:19

    Some progress can be made using a string-array, e.g.,

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

    couple with

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
      <string-array name="sports_array">
        <item>Shuttle Badminton</item>
        <item>Tennis</item>
        <item>FootBall</item>
      </string-array>
    </resources>
    

    source: http://theopentutorials.com/tutorials/android/listview/android-creating-and-populating-listview-items-in-xml/

    0 讨论(0)
  • 2021-02-05 01:27

    Actually there is a way! Re-using Layouts with include - Tag

    Simply define a Layout you want to reuse.

    <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"
         android:orientation="horizontal"/>
     <ImageView
            android:id="@+id/profileImage"
            android:layout_width="128dp"
            android:layout_height="128dp"
            android:src="@drawable/lena" />
     <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/profileOnline"/>
     </LinearLayout>
    

    Then include it in your ViewGroup as many times as required

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" 
        android:layout_width=”match_parent”
        android:layout_height=”match_parent”
        android:gravity="center_horizontal">
    
        <include layout="@layout/titlebar"/>
    
        <TextView android:layout_width=”match_parent”
                  android:layout_height="wrap_content"
                  android:text="@string/hello"
                  android:padding="10dp" />
    
        ...
    
    </LinearLayout>
    

    You can also override all the layout parameters (any android:layout_* attributes) of the included layout's root view by specifying them in the tag. For example:

    <include android:id=”@+id/news_title”
             android:layout_width=”match_parent”
             android:layout_height=”match_parent”
             layout=”@layout/title”/>
    

    More information at http://developer.android.com/training/improving-layouts/reusing-layouts.html

    0 讨论(0)
提交回复
热议问题