微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android:如何设置AlertDialog的宽度和高度,以及AlertDialog样式的按钮?

我有一个任务是通过xml更改AlertDialog的宽度和高度,我希望make成为样式,所以我可以轻松使用它.而且,我需要更改AlertDialog样式的按钮.你能告诉我一种方法来实现target.Thank你非常感激.
PS,我最好通过改变xml来实现目标.

解决方法

有两种方法1)以编程方式2)通过使用xml布局
1)=======>

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);
builder.setTitle("Title");
alertDialog = builder.create();
alertDialog.show();
alertDialog.getwindow().setLayout(600,400); //Controlling width and height.


                         ( or )

alertDialog.show();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();

lp.copyFrom(alertDialog.getwindow().getAttributes());
lp.width = 150;
lp.height = 500;
lp.x=-170;
lp.y=100;
alertDialog.getwindow().setAttributes(lp);

如果要显示显示的布局,如“警报”对话框,请参阅this

2)========>

choose.xml

<TextView
    android:id="@+id/img"
    android:layout_width="wrap_content"
    android:text="@string/choose"
    android:textSize="25dp"
    android:textColor="#fff"
    android:layout_height="50dp"/>

<TableLayout android:id="@+id/table"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#fff"
    android:orientation="vertical">

    <TableRow
        android:id="@+id/tr1" 
        android:orientation="horizontal"
        android:layout_margin="10dp">
        <ImageView 
            android:contentDescription="@string/phone"
            android:src="@drawable/phone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <TextView
            android:id="@+id/phnText"
            android:layout_width="wrap_content"
            android:text="@string/phone"
            android:gravity="left|center_vertical"
            android:layout_marginLeft="10dp"
            android:textSize="25dp"
            android:textColor="#000"
            android:layout_height="50dp"/>
    </TableRow>
    <View
            android:layout_width="fill_parent"
            android:layout_height="1dip"
            android:background="#FF000000" />

    <TableRow
        android:id="@+id/tr2" 
        android:orientation="horizontal"
        android:layout_margin="10dp">
        <ImageView 
            android:contentDescription="@string/sms"
            android:src="@drawable/sms"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <TextView
            android:id="@+id/smsText"
            android:layout_width="wrap_content"
            android:text="@string/sms"
            android:gravity="left|center_vertical"
            android:layout_marginLeft="10dp"
            android:textSize="25dp"
            android:textColor="#000"
            android:layout_height="50dp"/>
    </TableRow>

</TableLayout>
</LinearLayout>

将其显示为弹出窗口,如下所示

private void showPopUp()
{
    final AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
    helpBuilder.setTitle("");

    LayoutInflater inflater = getLayoutInflater();
    final View checkBoxLayout = inflater.inflate(R.layout.choose,null);
    helpBuilder.setView(checkBoxLayout);

    final AlertDialog helpDialog = helpBuilder.create();
    helpDialog.show();

    TableRow tablerowPhone  =    (TableRow)checkBoxLayout.findViewById(R.id.tr1);
    TableRow tablerowSms    =    (TableRow)checkBoxLayout.findViewById(R.id.tr2);

    tablerowPhone.setonClickListener(new OnClickListener() {

        public void onClick(View v) {

            helpDialog.dismiss();

            Uri callUri = Uri.parse("tel:" + listview_array[4]);  
            Intent intent = new Intent(Intent.ACTION_CALL,callUri); 
            startActivity(intent);
        }
    });

    tablerowSms.setonClickListener(new OnClickListener() {

        public void onClick(View v) {

            helpDialog.dismiss();

            Uri smsUri = Uri.parse("sms:" + listview_array[4]);  
            Intent intent = new Intent(Intent.ACTION_VIEW,smsUri); 
            startActivity(intent);
        }
    });
}

在你想要的地方调用这个showPopUp()方法.这样您就可以在xml文件中设置高度和宽度

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐