Styling Text in a Dialog List(在对话框列表中设置文本样式)
问题描述
我使用了此处给出的说明:.
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. :(
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;
Any help/direction is highly appreciated :)
At first I would create a list layout... something like:
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>
Then a simple TextView like:
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:
main
<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();
}
}
and that's all.
I tried to make that as simple as possible, you can google for ideas to make your list a bit attractive, like here.
这篇关于在对话框列表中设置文本样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在对话框列表中设置文本样式
基础教程推荐
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 降序排序:Java Map 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01