android开发设置dialog的高宽
这里设置为跟屏幕一样的宽度,:看代码
dlg.show(); WindowManager.LayoutParams params = dlg.getWindow().getAttributes(); params.width = this.getWindowManager().getDefaultDisplay().getWidth(); // params.height = 200 ; dlg.getWindow().setAttributes(params);
以下附上自定义dialog输入框代码:
import com.carspeak.client.R; import android.app.Dialog; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; /** * 自定义弹出输入框 * Created by Administrator on 2014/12/11. */ public class CusInputDialog extends Dialog { private EditText editText; private Button positiveButton, negativeButton; private TextView title; public CusInputDialog(Context context) { super(context, R.style.CusInputAlertDialog); setCustomDialog(); } private void setCustomDialog() { View mView = LayoutInflater.from(getContext()).inflate(R.layout.view_dialog_input, null); title = (TextView) mView.findViewById(R.id.title); editText = (EditText) mView.findViewById(R.id.et_value); positiveButton = (Button) mView.findViewById(R.id.positiveButton); negativeButton = (Button) mView.findViewById(R.id.negativeButton); super.setContentView(mView); } public View getEditText(){ return editText; } @Override public void setContentView(int layoutResID) { } @Override public void setContentView(View view, ViewGroup.LayoutParams params) { } @Override public void setContentView(View view) { } /** * * 确定键监听器 * * @param listener * */ public void setOnPositiveListener(View.OnClickListener listener){ positiveButton.setOnClickListener(listener); } /** * * 取消键监听器 * * @param listener * */ public void setOnNegativeListener(View.OnClickListener listener){ negativeButton.setOnClickListener(listener); } public void setTitle(String t) { title.setText(t); } public void setpositiveButtonTxt(String t) { positiveButton.setText(t); } public void setnegativeButtonTxt(String t) { negativeButton.setText(t); } }
style:
<style name="CusInputAlertDialog" parent="@android:style/Theme.Dialog"> <!-- 带输入框的自定义弹出框样式 --> <item name="android:windowNoTitle">true</item> <item name="android:background">@android:color/transparent</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item> </style>
layout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@drawable/bg_bombbox" android:orientation="vertical" android:paddingLeft="10dp" android:paddingRight="10dp" android:paddingBottom="10dp"> <TextView android:id="@+id/title" android:layout_width="fill_parent" android:layout_height="40.0dip" android:gravity="center" android:text="输入" android:singleLine="true" android:ellipsize="end" android:textColor="#fff" android:textSize="15sp" /> <EditText android:id="@+id/et_value" android:layout_width="fill_parent" android:layout_height="60dp" android:layout_marginTop="15dp" android:gravity="left|center" android:singleLine="true" android:maxLength="200" android:padding="1dp" android:textColor="#333"/> <!-- </LinearLayout> --> <LinearLayout android:layout_width="match_parent" android:layout_height="35.0dip" android:layout_gravity="bottom" android:layout_marginTop="20dp" android:gravity="center" android:orientation="horizontal"> <Button android:id="@+id/negativeButton" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:textColor="#fff" android:background="@drawable/selector_bt_gray2333" android:gravity="center" android:paddingLeft="8dp" android:paddingRight="8dp" android:paddingTop="5dp" android:paddingBottom="5dp" android:text="取消"/> <Button android:id="@+id/positiveButton" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:layout_marginLeft="20.0dip" android:textColor="#fff" android:background="@drawable/selector_bt_blue2deeper" android:gravity="center" android:paddingLeft="8dp" android:paddingRight="8dp" android:paddingTop="5dp" android:paddingBottom="5dp" android:text="确定"/> </LinearLayout> </LinearLayout>
使用方法:
final CusInputDialog dlg = new CusInputDialog(this); dlg.setTitle("请输入对应的语音命令"); dlg.setOnNegativeListener(new OnClickListener() { @Override public void onClick(View arg0) { dlg.dismiss(); } }); dlg.setOnPositiveListener(new OnClickListener() { @Override public void onClick(View arg0) { String command_txt = ((EditText)dlg.getEditText()).getText().toString(); Log.v("CharInputActivity", "!RegexUtils.IsVoiceCommand(command_txt)="+!RegexUtils.IsVoiceCommand(command_txt)); if(!StringUtil.isNullorEmpty(command_txt)&&RegexUtils.IsVoiceCommand(command_txt)) { // VoiceCommandEntity voice = new VoiceCommandEntity(); // voice.command = command_txt; // voice.preViewUri = saveProjectionImage2File(); // saveProjectionImage2DB(voice); // dlg.dismiss(); tv_shadow_preview.setDrawingCacheEnabled(true); tv_shadow_preview.buildDrawingCache(); //启用DrawingCache并创建位图 Bitmap screen = Bitmap.createBitmap(tv_shadow_preview.getDrawingCache()); //创建一个DrawingCache的拷贝,因为DrawingCache得到的位图在禁用后会被回收 tv_shadow_preview.setDrawingCacheEnabled(false); //禁用DrawingCahce否则会影响性能 SaveVoiceCommandUtil sv = new SaveVoiceCommandUtil(CharInputActivity.this); int rs = sv.saveVoiceCommand(command_txt, screen); if(rs==1){Toast.makeText(CharInputActivity.this, "保存成功", Toast.LENGTH_SHORT).show();dlg.dismiss();} else if(rs==0){Toast.makeText(CharInputActivity.this, "该命令已经存在", Toast.LENGTH_SHORT).show();} else {Toast.makeText(CharInputActivity.this, "保存失败,错误码:"+rs, Toast.LENGTH_SHORT).show();} } else { Toast.makeText(CharInputActivity.this, "命令词只能是中英文和数字", Toast.LENGTH_SHORT).show(); } } }); dlg.show(); WindowManager.LayoutParams params = dlg.getWindow().getAttributes(); params.width = this.getWindowManager().getDefaultDisplay().getWidth()-DensityUtils.dp2px(this, 40); // params.height = 200 ; dlg.getWindow().setAttributes(params);