在对话框列表样式文本 [英] Styling Text in a Dialog List

查看:110
本文介绍了在对话框列表样式文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经给这里的说明: http://developer.android .COM /引导/主题/ UI / dialogs.html#AddingAList 创造一个对话列表。

I used the instructions given Here: http://developer.android.com/guide/topics/ui/dialogs.html#AddingAList to create a List in a dialog.

问题是,我似乎并没有找到一个方法来包装里面的选项长文本。 [请参见下图]

The problem is I don't seem to find out a way to wrap long text inside the options. [Please see the image below]

请告诉我该怎么做文字换行。 :(

Please tell me how to do the text wrapping. :(

我使用下面的code:

I am using the following code:

items= getArrayFromJson(source+"getdetails.php?brand="+bName+"&car="+cName);
        builder = new AlertDialog.Builder(this);
        builder.setTitle("Choose Model");
        builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                if(items[item].equals("No Options Available")){
                    dismissDialog(2);
                }
                else{
                    model.setText(items[item]);
                    mName= items[item].toString();

                    location.setEnabled(true);
                    dismissDialog(2);
                }

            }
        });
        alert = builder.create();

        return alert;

任何帮助/方向是非常AP preciated:)

Any help/direction is highly appreciated :)

推荐答案

起初,我将创建一个列表布局...是这样的:

At first I would create a list layout... something like:

list_layout

list_layout

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

     <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp" />
 </LinearLayout>

然后一个简单的TextView这样的:

Then a simple TextView like:

single_item_layout

single_item_layout

 <LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"> 

     <TextView android:id="@+id/singleItem"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:textStyle="bold"
        android:textSize="22dp"
        android:textColor="#FFFFFF"
        android:padding="10dp"
        android:text="blue thingy"
        android:background="#336699" />

</LinearLayout>

和一个简单的主要布局:

and a simple main layout:

主要

    <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" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:onClick="popUp"
        android:text="pop dialog list" />

</RelativeLayout>

最后一个简单的主要活动:

lastly a simple main Activity:

import java.util.ArrayList;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void popUp(View v){

        // Dummy list:
        ArrayList<String> dummies = new ArrayList<String>();

        dummies.add("dumm1");
        dummies.add("dumm2");
        dummies.add("dumm3");
        dummies.add("dumm4");
        dummies.add("dumm5");

        final Dialog dialog = new Dialog(MainActivity.this);
        dialog.setContentView(R.layout.list_layout);
        dialog.setTitle("List Title");
        ListView listView = (ListView) dialog.findViewById(R.id.list);

        ArrayAdapter<String> ad = new ArrayAdapter<String>(this, R.layout.single_item_layout , R.id.singleItem, dummies);
        listView.setAdapter(ad);

        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                //do something on click
                dialog.dismiss();
            }
        });

        dialog.show();
    }   
}

而这一切。

我试图使这一尽可能简单,你可以谷歌的想法,使您的列表有点吸引力的,像<一个href="http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/">here.

I tried to make that as simple as possible, you can google for ideas to make your list a bit attractive, like here.

这篇关于在对话框列表样式文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆