android,提示两个字怎么弄到左上方

YXTS122 2017-11-29 12:39:16

public class CustomDialog extends Dialog {  

public CustomDialog(Context context) {
super(context);
}

public CustomDialog(Context context, int theme) {
super(context, theme);
}

public static class Builder {
private Context context;
private String title;
private String message;
private String positiveButtonText;
private String negativeButtonText;
private View contentView;
private DialogInterface.OnClickListener positiveButtonClickListener;
private DialogInterface.OnClickListener negativeButtonClickListener;

public Builder(Context context) {
this.context = context;
}

public Builder setMessage(String message) {
this.message = message;
return this;
}

/**
* Set the Dialog message from resource
*
* @param title
* @return
*/
public Builder setMessage(int message) {
this.message = (String) context.getText(message);
return this;
}

/**
* Set the Dialog title from resource
*
* @param title
* @return
*/
public Builder setTitle(int title) {
this.title = (String) context.getText(title);
return this;
}

/**
* Set the Dialog title from String
*
* @param title
* @return
*/

public Builder setTitle(String title) {
this.title = title;
return this;
}

public Builder setContentView(View v) {
this.contentView = v;
return this;
}

/**
* Set the positive button resource and it's listener
*
* @param positiveButtonText
* @return
*/
public Builder setPositiveButton(int positiveButtonText,
DialogInterface.OnClickListener listener) {
this.positiveButtonText = (String) context
.getText(positiveButtonText);
this.positiveButtonClickListener = listener;
return this;
}

public Builder setPositiveButton(String positiveButtonText,
DialogInterface.OnClickListener listener) {
this.positiveButtonText = positiveButtonText;
this.positiveButtonClickListener = listener;
return this;
}

public Builder setNegativeButton(int negativeButtonText,
DialogInterface.OnClickListener listener) {
this.negativeButtonText = (String) context
.getText(negativeButtonText);
this.negativeButtonClickListener = listener;
return this;
}

public Builder setNegativeButton(String negativeButtonText,
DialogInterface.OnClickListener listener) {
this.negativeButtonText = negativeButtonText;
this.negativeButtonClickListener = listener;
return this;
}

public CustomDialog create() {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// instantiate the dialog with the custom Theme
final CustomDialog dialog = new CustomDialog(context);
View layout = inflater.inflate(R.layout.dialog_normal_layout, null);
dialog.addContentView(layout, new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
// set the dialog title
((TextView) layout.findViewById(R.id.title)).setText(title);
// set the confirm button
if (positiveButtonText != null) {
((Button) layout.findViewById(R.id.positiveButton))
.setText(positiveButtonText);
if (positiveButtonClickListener != null) {
((Button) layout.findViewById(R.id.positiveButton))
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
positiveButtonClickListener.onClick(dialog,
DialogInterface.BUTTON_POSITIVE);
}
});
}
} else {
// if no confirm button just set the visibility to GONE
layout.findViewById(R.id.positiveButton).setVisibility(
View.GONE);
}
// set the cancel button
if (negativeButtonText != null) {
((Button) layout.findViewById(R.id.negativeButton))
.setText(negativeButtonText);
if (negativeButtonClickListener != null) {
((Button) layout.findViewById(R.id.negativeButton))
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
negativeButtonClickListener.onClick(dialog,
DialogInterface.BUTTON_NEGATIVE);
}
});
}
} else {
// if no confirm button just set the visibility to GONE
layout.findViewById(R.id.negativeButton).setVisibility(
View.GONE);
}
// set the content message
if (message != null) {
((TextView) layout.findViewById(R.id.message)).setText(message);
} else if (contentView != null) {
// if no message set
// add the contentView to the dialog body
((LinearLayout) layout.findViewById(R.id.content))
.removeAllViews();
((LinearLayout) layout.findViewById(R.id.content))
.addView(contentView, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
}
dialog.setContentView(layout);
return dialog;
}
}
}



...全文
168 5 点赞 打赏 收藏 举报
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
elsemind 2017-11-30
使用自定义的View,然后使用dialog的setContentView方法就行了
  • 打赏
  • 举报
回复
雨焰 2017-11-30
自定义一个布局文件赋予dialog试试
  • 打赏
  • 举报
回复
YXTS122 2017-11-29
dialog_normal_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:clickable="true"  
    android:orientation="vertical"  
    android:padding="20.0dip" >  
  
    <LinearLayout  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:layout_gravity="center"   
        android:orientation="vertical" >  
  
        <TextView  
            android:id="@+id/title"   
            android:layout_width="fill_parent"  
            android:layout_height="40.0dip"  
            android:gravity="center"  
            android:text="title_alert"  
            android:visibility="visible" />  
  
        <LinearLayout  
            android:id="@+id/content"  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:gravity="center" >  
  
  
            <TextView  
                android:id="@+id/message" 
                android:layout_width="fill_parent"  
                android:layout_height="wrap_content"  
                android:gravity="left|center"  
                android:lineSpacingMultiplier="1.5"  
                android:minHeight="120.0dip"  
                android:paddingBottom="15.0dip"  
                android:paddingLeft="20.0dip"  
                android:paddingRight="20.0dip"  
                android:paddingTop="15.0dip" />  
        </LinearLayout>  
  
        <View  
            android:layout_width="fill_parent"  
            android:layout_height="1.0px"  
            android:background="#ffd0d0d0" />  
  
        <LinearLayout  
            android:layout_width="fill_parent"  
            android:layout_height="60.0dip"  
            android:layout_gravity="bottom"  
            android:gravity="center"  
            android:orientation="horizontal" >  
  
            <Button  
                android:id="@+id/positiveButton"  
                android:layout_width="114.0dip"  
                android:layout_height="40.0dip"    
                android:gravity="center"  
                android:text="ok" />  
  
            <Button  
                android:id="@+id/negativeButton"  
                android:layout_width="114.0dip"  
                android:layout_height="40.0dip"  
                android:layout_marginLeft="20.0dip"  
                android:gravity="center"  
                android:text="取消" />  
        </LinearLayout>  
    </LinearLayout>  
  
</FrameLayout>  
  
public class MainActivity extends Activity implements OnClickListener {
	Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=(Button)findViewById(R.id.bt1);
        button.setOnClickListener(this);
    }
         
     
     
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.bt1:
        	new CustomDialog.Builder(MainActivity.this)//此处括号中不能使用mcontext,应为他直接
			//通过getApplicationContext()获得的Context,而必须使用Activity,因为只有一个Activity才能添加一个窗体。
	        .setMessage("确定删除联系人吗?") 
	        .setTitle("提示")  
	        .setPositiveButton("确定", new DialogInterface.OnClickListener() {  
	            public void onClick(DialogInterface dialog, int which) {  
	                dialog.dismiss();  
	            }
	        })
	        .setNegativeButton("取消",  
	                new DialogInterface.OnClickListener() {  
	                    public void onClick(DialogInterface dialog, int which) {  
	                        dialog.dismiss();  
	                    }  
	                }).create().show();  
	  
            break;
        default:break;
             
        }
    }
     
  
  • 打赏
  • 举报
回复
ganshenml 2017-11-29
不使用自带的title,自定义contentView的textView
  • 打赏
  • 举报
回复
moonFY 2017-11-29
看的头有点疼,把你的写的 关于title 的去掉 试试。
  • 打赏
  • 举报
回复
相关推荐
发帖
Android
加入

7.9w+

社区成员

移动平台 Android
申请成为版主
帖子事件
创建了帖子
2017-11-29 12:39
社区公告
暂无公告