AlertDialog自己定义View的使用方法+怎样改变弹出框的大小

android系统定义了弹出框,支持我们自己定义布局:

	public AlertDialog getEditCustomDialog() {
		LayoutInflater inflater = getLayoutInflater();
		View view = inflater.inflate(R.layout.custom_message_rename, null);
		AlertDialog.Builder builder = new AlertDialog.Builder(AnimationTest.this);
		builder.setView(view);
		builder.setTitle("A New Version is Available");
		return builder.create();
	}

<?

xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <EditText android:id="@+id/rc_document_edit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:imeOptions="flagNoEnterAction" android:inputType="textNoSuggestions" android:maxLines="5" android:textColor="#000000" android:textSize="@dimen/font_size_medium" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginTop="10dp" android:background="?

android:attr/dividerHorizontal" /> <LinearLayout android:id="@+id/LinearLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:measureWithLargestChild="true" android:orientation="horizontal" android:padding="0dp" > <Button android:id="@+id/cancelBtn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="?android:attr/selectableItemBackground" android:text="Cancel" /> <View android:id="@+id/postCancelBtnDivider" android:layout_width="1dp" android:layout_height="match_parent" android:background="?android:attr/dividerHorizontal" /> <Button android:id="@+id/okBtn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="?

android:attr/selectableItemBackground" android:text="OK" /> </LinearLayout> </LinearLayout>


效果图是:

 



另一种常见的样式是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/title"
    android:layout_width="match_parent"
    android:layout_height="60dp" >

    <TextView
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_marginTop="20dp"
        android:layout_height="60dp"
        android:layout_marginLeft="25dp"
        android:text="@string/upgrade_content"
        android:textColor="#000000"
        android:textSize="22sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_below="@id/content"
        android:orientation="horizontal" >

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="15dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="15dp"
            android:text="@string/remind_me"
            android:textColor="#000000"
            android:textSize="14sp" />
    </LinearLayout>

</RelativeLayout>

效果图是:



假设想改变Dialog的大小能够这样写:

	<span style="white-space:pre">				</span>AlertDialog dialog = getCustomDialog();
					dialog.show();
					
					//一定得在show完dialog后来set属性
					WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
					lp.width = AnimationTest.this.getResources().getDimensionPixelSize(R.dimen.dialog_width);
					lp.height = AnimationTest.this.getResources().getDimensionPixelSize(R.dimen.dialog_height);
					dialog.getWindow().setAttributes(lp);


posted on 2017-06-04 08:57 mthoutai 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/mthoutai/p/6939499.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值