【问题标题】:Android AlertDialog underline text in listAndroid AlertDialog 列表中的文本下划线
【发布时间】:2014-08-01 12:01:54
【问题描述】:

我有一个 AlertDialog,它在列表中显示不同的项目。现在我希望这些项目中的一些是斜体和一些下划线。这怎么可能?我真的需要创建一个自定义的 AlertDialog 吗?

我的代码:

    ArrayList<String> names = new ArrayList<String>();
    String[] splitted = response.split("\\|");
    for(String name:splitted) {
        names.add(name.split("\\+")[1]);
    }
    String[] items = new String[names.size()];
    items = names.toArray(items);
    AlertDialog.Builder builder = new AlertDialog.Builder(ChatActivity.this);
    builder.setTitle("Onlineliste");
    builder.setItems(items, null);
    AlertDialog alert = builder.create();
    alert.show();    

【问题讨论】:

    标签: java android android-alertdialog underline


    【解决方案1】:

    是的,您必须创建一个自定义警报对话框

    对于下划线

    textview.setPaintFlags(textview.getPaintFlags()|Paint.UNDERLINE_TEXT_FLAG)
    

    斜体

    textView.setTypeface(null, Typeface.ITALIC)
    

    【讨论】:

    • 没有TextView?!
    • 您必须创建一个自定义对话框,并根据您在对话框中的要求设置下划线和斜体
    • 但是我也需要一个服装Listview,我该怎么做?
    • 是的,只有一个文本视图的自定义列表视图,并根据您的要求设置上述文本视图的属性。
    • 当我想要 1 项斜体和 1 项下划线时,情况如何?只有一个文本视图是不可能的:/
    【解决方案2】:

    试试这个

     public class Common_SingleLine_Alert extends Dialog implements
            View.OnClickListener {
    
        Button okBtn;
        TextView messagetv;
        String messageStr;
    
        public Common_SingleLine_Alert(Context context, Spannable message) {
            super(context);
            // TODO Auto-generated constructor stub
            this.messageStr = message;
    
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.marker_info_window);
    
            okBtn = (Button) findViewById(R.id.okBtn);
            messagetv = (TextView) findViewById(R.id.address);
    
            messagetv.setText(messageStr);
    
            okBtn.setOnClickListener(this);
        }
    
        public void onClick(View v) {
            // TODO Auto-generated method stub
    
            dismiss();
    
        }
    
    }
    

    // xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:background="@color/white">
    
        <RelativeLayout
            android:id="@+id/relativeLayout1"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:background="@color/themeColor" >
    
            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:text="@string/title"
                android:textColor="@color/white"
                android:textStyle="bold" />
        </RelativeLayout>
    
        <TextView
            android:id="@+id/address"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/relativeLayout1"
            android:layout_centerHorizontal="true"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:gravity="center_horizontal"
            android:text="TextView" />
    
    
    
            <Button
                android:id="@+id/okBtn"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="100dp"
                android:layout_height="30dp"
                android:layout_below="@+id/address"
                android:layout_centerHorizontal="true"
                android:background="@drawable/reverse_common_button_selection"
                android:text="@android:string/ok" 
                android:layout_marginTop="5dp"/>
    
    
    </RelativeLayout>
    

    // 怎么用

    dialog = new Common_SingleLine_Alert(this, getResources()
                        .getString(Html.fromHtml("<i>Name</i>")));
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setCancelable(true);
                dialog.setOnDismissListener(new Dialog.OnDismissListener() {
    
                    public void onDismiss(DialogInterface d) {
                        // TODO Auto-generated method stub
                    }
                });
                dialog.show();
    

    【讨论】:

    • 我不是指标题,我是指不同的项目
    • Html.fromHtml("标题
      此处描述").toString();
    • 请看,有 .setItems(items, null);并且没有办法做到这一点 Html.fromHtml
    • 您已经创建了一个字符串的 ArrayList,只需使用此 Html.fromHtml("Title
      Description here") 创建一个 Array 列表。 toString()
    • 对不起,我没有认出 toString() 但这根本没有任何作用,没有样式或任何东西
    猜你喜欢
    • 2011-05-06
    • 1970-01-01
    • 2013-08-22
    • 1970-01-01
    • 2021-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-07
    相关资源
    最近更新 更多