在android中,ListView包含TextView,以及如何为TextView

时间:2015-06-28 04:41:35

标签: android-listview

我有一个带有TextView的ListItem。我将TextView的minHeight设置如下。

list_item_forecast.xml

<TextView ?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id = "@+id/list_item_forecast_textView"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
/>

和片段文件xml如下。

fragment_main.xml

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".ForecastFragment">

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

即使我将textView的minHeight设置为“?android:attr / listPreferredItemHeight”,我看到设备上的输出如下。

这是fragment.java中的代码。需要在ListView中显示forecastArrayAdapter字符串数据

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    String[] forecastArray = new String[]{"Today - Sunny - 86/83",
            "Tomorrow - Foggy - 70/46",
            "Weds - Cloudy - 72/63",
            "Thurs - Rainy - 64/51",
            "Fri - Foggy - 70/46",
            "Sat - Sunny - 76/68",
            "Sun - Sunny - 20/7"};

    List<String> weekForecast = new ArrayList<String>(Arrays.asList(forecastArray));

    ArrayAdapter<String> mForecastAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_forecast,
                                                                    R.id.list_item_forecast_textView,weekForecast );

    ListView listView =  (ListView) rootView.findViewById(R.id.listView_forecast);
    listView.setAdapter(mForecastAdapter);
    return rootView;
}

有人可以帮我设置每个文本的minHeight吗?

由于

1 个答案:

答案 0 :(得分:0)

虽然我在 list_item_forcast.xml

中看不到任何TextView

在文字视图中,您必须添加: android:minLines = "3"android:maxLines = "3"

它会将textView的高度保持为3行。

示例TextView将如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="6dip" >
    <TextView
       android:id="@+id/tv_addesc"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:scrollbars="vertical"
       android:maxLines="3"
       android:minLines="3"
       android:textColor="#FFFFFFFF"
       android:textSize="15sp" />
</RelativeLayout>

注意:如果字符数超过3行限制,则会显示如下文字:

” 你好世界

Hello world

Hello world ......“