我想创建片段,它将静态菜单显示为列表中的一组行.
我喜欢在tableview中使用静态单元格的iOS方法.我怎样才能在android中实现这一点(所以不需要代码来定义元素,只需要xml)
有没有常规的方法来下一步在xml中定义静态元素
(伪代码)
list_view.xml
<List view>
- use element my_row with onclick=row1_clicked and title="row 1"
- use element my_row with onclick=row2_clicked and title="row 2"
- use element my_row with onclick=row3_clicked and title="row 3"
- use element my_row with onclick=row4_clicked and title="row 4"
</List view>
my_row.xml
<My Row>
- text field (title should go here)
- on click (on click should go here)
</My Row>
Run Code Online (Sandbox Code Playgroud)
所以基本上我想在列表中"包含"行并在xml级别上执行(无代码).
Nik*_*Nik 24
不幸的是,禁止通过xml列表视图定义.不过需要使用适配器混乱.
use*_*768 21
使用字符串数组可以取得一些进展,例如,
<?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>
Run Code Online (Sandbox Code Playgroud)
夫妇
<?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>
Run Code Online (Sandbox Code Playgroud)
来源:http://theopentutorials.com/tutorials/android/listview/android-creating-and-populating-listview-items-in-xml/
Eri*_*ikR 14
您可以使用嵌套在ScrollView中的垂直LinearLayout来获取您要查找的内容.将"listview"项放在LinearLayout中,并将它们设置为类似于listview的元素.
如果您绝对必须使用ListView(例如,您正在使用ListFragment),则必须使用ListAdapter子类或滚动自己的子类.
其实还有办法!使用include - Tag 重新使用布局
只需定义要重用的布局即可.
<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>
Run Code Online (Sandbox Code Playgroud)
然后根据需要将其包含在ViewGroup中多次
<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>
Run Code Online (Sandbox Code Playgroud)
您还可以通过在标记中指定它们来覆盖所包含布局的根视图的所有布局参数(任何android:layout_*属性).例如:
<include android:id=”@+id/news_title”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
layout=”@layout/title”/>
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请访问http://developer.android.com/training/improving-layouts/reusing-layouts.html
| 归档时间: |
|
| 查看次数: |
27699 次 |
| 最近记录: |