android 自定义dialog用法,实现对话框内容自定义

android的对话框dialog,不能实现位置的自定义。只能定义好大小,让dialog的主题自动适配。如果需要自定义位置试试popwindow。

直接上代码:

1、自定义的dialog

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

public class FeedBackDiag extends Dialog implements OnClickListener
{
    Context ctx;
    
    public FeedBackDiag(Context ctx)
    {
        super(ctx);
        this.ctx = ctx;
    }
    
    private View IBCancer;
    
    private View IBSubmit;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.feedback);
        IBCancer = findViewById(R.id.feedbace_cancer);
        IBSubmit = findViewById(R.id.feedbace_submit);
        
        IBCancer.setOnClickListener(this);
        IBSubmit.setOnClickListener(this);
    }
    
    @Override
    public void onClick(View v)
    {
        if (v == IBCancer)
        {    <pre name="code" class="java">            FeedBackDiag.<span style="font-family: Arial, Helvetica, sans-serif;">this.dismiss();</span><span style="font-family: Arial, Helvetica, sans-serif;">      </span>
} else if (v == IBSubmit) {

 
        }
        
    }
    
}


2、自定义dialog的view xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="260dp"
    android:layout_height="235dp"
    android:background="@color/white"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:gravity="center_vertical"
        android:layout_marginLeft="7dp"
        android:padding="10dp"
        android:text="意见反馈"
        android:textColor="#42bd41"
        android:textSize="16dp" />

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="2px"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="#cccccc" />

    <EditText
        android:id="@+id/feedbackcontent"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:layout_margin="10dp"
        android:background="@drawable/input_bg"
        android:gravity="top"
        android:hint="请留下您宝贵的建议或意见。。。"
        android:textColorHint="#999999"
        android:inputType="textMultiLine"
        android:padding="10dp"
        android:textSize="14dp" >

        <requestFocus android:layout_width="wrap_content" />
    </EditText>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="2px"
        android:background="#cccccc" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="44dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/feedbace_cancer"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="@drawable/selector_ffffff_eaeaea"
            android:text="取消"
            android:textColor="#222222"
            android:textSize="14dp" />

        <ImageView
            android:layout_width="2px"
            android:layout_height="fill_parent"
            android:layout_marginLeft="0dp"
            android:layout_marginRight="0dp"
            android:background="#cccccc" />

        <Button
            android:id="@+id/feedbace_submit"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="@drawable/selector_ffffff_eaeaea"
            android:text="确定"
            android:textColor="#222222"
            android:textSize="14dp" />
    </LinearLayout>

</LinearLayout>


3、在activity 中使用自定义的dialog


new FeedBackDiag(SettingActivity.this).show();

也可以通过activity 使用对话框主题来做同样的效果,不过如果需要返回对话框中的监听,代码中就显示有点复杂,并且自定义的dialog的布局可以当做一个activity来设计,相对来说足够使用了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值