gpt4 book ai didi

android - android.R.layout.simple_list_item_1 和 android.R.layout.simple_list_item_2 有什么区别

转载 作者:IT老高 更新时间:2023-10-28 22:21:41 35 4
gpt4 key购买 nike

谁能解释android中arrayadapter中的android.R.layout.simple_list_item_1和android.R.layout.simple_list_item_2。

我知道 android.R.layout.simple_list_item_1 和 android.R.layout.simple_list_item_2 是 android 本身定义的布局。

在 android.R.layout.simple_list_item_1 中只包含一个 textview 而 android.R.layout.simple_list_item_2 包含两个 text view。

我想以 android.R.layout.simple_list_item_2 为例...如何使用适配器在 ListView 中显示两个 TextView 。

我的代码是

package com.app.listview;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ExampleListViewActivity extends Activity {

private String[] nameArr = new String[]{"Arun","Anil","Ankit","Manoj"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ListView listView = (ListView)findViewById(R.id.lv);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
android.R.id.text1,
nameArr);
listView.setAdapter(adapter);
}
}

最佳答案

区别如下。 simple_list_item_1 只包含一个 TextView,而 simple_list_item_2RelativeLayout 的子类中有两个。这些都取自果冻 bean 。

simple_list_item_1

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
android:paddingRight="?android:attr/listPreferredItemPaddingRight"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
/>

simple_list_item_2

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:mode="twoLine"
>

<TextView android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="?android:attr/listPreferredItemPaddingLeft"
android:layout_marginTop="8dip"
android:textAppearance="?android:attr/textAppearanceListItem"
/>

<TextView android:id="@android:id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@android:id/text1"
android:layout_alignLeft="@android:id/text1"
android:textAppearance="?android:attr/textAppearanceSmall"
/>

</TwoLineListItem>

根据docs for ArrayAdapter :

By default this class expects that the provided resource id references a single TextView.

所以默认情况下,一个 ArrayAdapter 不会自动填充多个 TextView 实例。但是,您可以覆盖 getView() 方法并填写出现在 R.layout.simple_list_item_2

TextView >

关于android - android.R.layout.simple_list_item_1 和 android.R.layout.simple_list_item_2 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11722885/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com