安卓:我怎样才能设置AlertDialog的宽度和高度,以及AlertDialog风格的按钮? [英] Android:How can I set the AlertDialog width and height,and the button of the AlertDialog style?

查看:155
本文介绍了安卓:我怎样才能设置AlertDialog的宽度和高度,以及AlertDialog风格的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个任务来更改AlertDialog的宽度和高度由XML,我想使之成为风格,所以我可以用它easy.And,我需要改变的AlertDialog样式的按钮also.Can你能告诉我一个方法实现target.Thank你非常感谢。 PS,我会更好地改变XML实现目标。

I have a task to change the AlertDialog width and height by xml,I want make that become style,so I can use it easy.And that ,I need to change button of AlertDialog style also.Can you tell me a way to achieve the target。Thank you Very grateful。 PS,I'd better achieve the target by change xml。

推荐答案

有两种方法1)编程2)通过使用XML布局

There are two methods 1) programatically 2) By using xml layout

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);

如果你想显示的布局像警报对话框将显示

If you want to show the layout to be displayed like Alert dialog see this

2)========>

choose.xml

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>

作为弹出显示该像下面的

display this as popup as like below

 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文件中的布局

call this showPopUp() method where you want. so that you can set height and width to the layout in xml file

这篇关于安卓:我怎样才能设置AlertDialog的宽度和高度,以及AlertDialog风格的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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