如何将一个activity对话框样式设置成窗口的样式

4412人阅读
Android(12)
1.在AndroidManifest.xml文件中设置当前需要改变成窗口样式的Activity的属性,即
android:theme=&@android:style/Theme.Dialog&或者
android:theme=&@android:style/Theme.Holo.Dialog&2.在styles.xml文件中自定义一个主题样式,改主题样式必须继承Dialog的样式。然后在AndroidManifest.xml文件中引用你自定义的主题即可。
&style name=&Theme.MyDialog& parent=&android:style/Theme.Dialog&&
&item name=&android:windowBackground&&@drawable/my_dialog&/item&
&/style&在drawable中新建my_dialog.xml文件,内容如下:
&?xml version=&1.0& encoding=&utf-8&?&
&shape xmlns:android=&/apk/res/android&&
&solid android:color=&#ffffff& /&
android:width=&3dp&
android:color=&@android:color/holo_blue_light& /&
&corners android:radius=&3dp& /&
android:left=&10dp&
android:top=&10dp&
android:right=&10dp&
android:bottom=&10dp& /&
&/shape&在AndroidManifest.xml文件中设置当前需要改变成窗口样式的Activity的属性,即
android:theme=&@style/Theme.MyDialog&
&&相关文章推荐
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:375120次
积分:2812
积分:2812
排名:第11758名
原创:45篇
评论:51条[转]如何利用Activity的Dialog风格完成弹出框设计 - freeliver54 - 博客园
随笔 - 2445, 文章 - 3, 评论 - 2318, 引用 - 157
本文转自:
在我们使用Dialog时,如果需要用到很多自己设计的控件,虽然可以让弹出框显示出我们需要的界面,但却无法找到地方完成控制代码的编写,如何解决这个问题呢,我们可以将Activity伪装成Dialog弹出框,这样即显示了界面,在Activity里写控制代码也是大家的拿手好戏了,现在我就来抛砖引玉说说简单的实现吧。
首先,问题的关键在MainActivity里的一句&android:theme="@android:style/Theme.Dialog",这就是Activity的Dialog风格。
我们先创建一个main.xml,内容如下
&version="1.0"&encoding="utf-8"&&&xmlns:android="/apk/res/android"&&&&&&android:orientation="vertical"&&&&&&android:layout_width="fill_parent"&&&&&&android:layout_height="fill_parent"&&&&&&&&&&&&&&&&android:id="@+id/showString"&&&&&&android:layout_width="fill_parent"&&&&&&&android:layout_height="wrap_content"&&&&&&&android:text="在这里显示dialog里输入的数字:"&&&&&&&&&&&&&&&&&android:id="@+id/openButton"&&&&&&android:text="点此打开Dialog"&&&&&&android:layout_width="fill_parent"&&&&&&android:layout_height="wrap_content"&&&&&&&&&&
&?xml version="1.0" encoding="utf-8"?&
&LinearLayout xmlns:android="/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/showString"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="在这里显示dialog里输入的数字:"
android:id="@+id/openButton"
android:text="点此打开Dialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
&/LinearLayout&再创建一个textdialog.xml,内容如下
&version="1.0"&encoding="utf-8"&&&&&&xmlns:android="/apk/res/android"&&&&android:orientation="vertical"&&&&android:layout_width="match_parent"&&&&android:layout_height="match_parent"&&&&&&&&&android:id="@+id/et"&&&&&&android:layout_width="fill_parent"&&&&&&android:layout_height="wrap_content"&&&&&&&&&&&&&&&&&&android:id="@+id/returnButton"&&&&&&android:text="请输入字符"&&&&&&android:layout_width="fill_parent"&&&&&&android:layout_height="wrap_content"&&&&&&&&&&&&&&
&?xml version="1.0" encoding="utf-8"?&
&LinearLayout
xmlns:android="/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"&
android:id="@+id/et"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/returnButton"
android:text="请输入字符"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
&/LinearLayout&
现在在MainActivity里写下如下代码,都是很基本的代码,相信大家都能看懂&style="font-size:16px"&style="font-size:16px"&name="code"&class="java"public&class&MainActivity&extends&Activity&{&&&&&&&&&&&&private&Button&openB&&&&&&private&TextView&showS&&&&&&&&&&&&public&void&onCreate(Bundle&savedInstanceState)&{&&&&&&&&&&super.onCreate(savedInstanceState);&&&&&&&&&&setContentView(R.layout.main);&&&&&&&&&&&&&&&&&&&&openButton&=&(Button)findViewById(R.id.openButton);&&&&&&&&&&showString&=&(TextView)findViewById(R.id.showString);&&&&&&&&&&&&&&&&&&&&openButton.setOnClickListener(new&OnClickListener()&{&&&&&&&&&&&&&&&&&&&&&&&&public&void&onClick(View&v)&{&&&&&&&&&&&&&&&&&&//这里用到了返回试Activity的基本用法,因为和主题无关,就不多解释了&&&&&&&&&&&&&&&&&&Intent&i&=&new&Intent(MainActivity.this,&testDialog.class);&&&&&&&&&&&&&&&&&&startActivityForResult(i,&0);&&&&&&&&&&&&&&}&&&&&&&&&&});&&&&&&&&&&&&&&&&}&&&&&&&&&&&&//利用返回试Activity接收输入的数据并显示,证明我们的Dialog式的Activity确实可以完成数据的处理&&&&&&protected&void&onActivityResult(int&requestCode,&int&resultCode,&Intent&data)&{&&&&&&&&&&super.onActivityResult(requestCode,&resultCode,&data);&&&&&&&&&&//取出字符串&&&&&&&&&&Bundle&bundle&=&data.getExtras();&&&&&&&&&&String&str&=&bundle.getString("str");&&&&&&&&&&showString.setText(str);&&&&&&}&&}&&
&/pre&现在在MainActivity里写下如下代码,都是很基本的代码,相信大家都能看懂&p&&/p&&p&&span style="font-size:16px"&&span style="font-size:16px"&&/span&&/span&&/p&&pre name="code" class="java"&public class MainActivity extends Activity {
private Button openB
private TextView showS
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
openButton = (Button)findViewById(R.id.openButton);
showString = (TextView)findViewById(R.id.showString);
openButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//这里用到了返回试Activity的基本用法,因为和主题无关,就不多解释了
Intent i = new Intent(MainActivity.this, testDialog.class);
startActivityForResult(i, 0);
//利用返回试Activity接收输入的数据并显示,证明我们的Dialog式的Activity确实可以完成数据的处理
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//取出字符串
Bundle bundle = data.getExtras();
String str = bundle.getString("str");
showString.setText(str);
}下面是testDialog的编程,你可以看出这个Dialog和正常的Activity就没什么区别,但它最后确实可以像Dialog一样弹出
ublic&class&testDialog&extends&Activity{&&&&&&&&&&&&private&Button&returnB&&&&&&private&EditText&inputE&&&&&&&&&&&&protected&void&onCreate(Bundle&savedInstanceState)&{&&&&&&&&&&super.onCreate(savedInstanceState);&&&&&&&&&&setContentView(R.layout.textdialog);&&&&&&&&&&&&&&&&&&&&returnButton&=&(Button)findViewById(R.id.returnButton);&&&&&&&&&&inputEditor&=&(EditText)findViewById(R.id.et);&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&returnButton.setOnClickListener(new&OnClickListener()&{&&&&&&&&&&&&&&public&void&onClick(View&v)&{&&&&&&&&&&&&&&&&&&String&input&=&inputEditor.getText().toString();&&&&&&&&&&&&&&&&&&Intent&i&=&new&Intent(testDialog.this,&MainActivity.class);&&&&&&&&&&&&&&&&&&Bundle&b&=&new&Bundle();&&&&&&&&&&&&&&&&&&b.putString("str",&input);&&&&&&&&&&&&&&&&&&i.putExtras(b);&&&&&&&&&&&&&&&&&&testDialog.this.setResult(RESULT_OK,&i);&&&&&&&&&&&&&&&&&&testDialog.this.finish();&&&&&&&&&&&&&&}&&&&&&&&&&});&&&&&&}&&}&&
ublic class testDialog extends Activity{
private Button returnB
private EditText inputE
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.textdialog);
returnButton = (Button)findViewById(R.id.returnButton);
inputEditor = (EditText)findViewById(R.id.et);
//和前面一样,只是用到了返回式Activity的基本方法,虽然这里已经是个Dialog了,但却和普通Activity无异
returnButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String input = inputEditor.getText().toString();
Intent i = new Intent(testDialog.this, MainActivity.class);
Bundle b = new Bundle();
b.putString("str", input);
i.putExtras(b);
testDialog.this.setResult(RESULT_OK, i);
testDialog.this.finish();
最后的亮点,设置Activity的Dialog风格,在MainActivity里注册下第二个Activity吧,别完了风格设置哦
&activity&android:name=".testDialog"&&&&&&&&&&&&&&&&android:label="这是一个Activity变成的Dialog"&&&&&&&&&&&&&&&&android:theme="@android:style/Theme.Dialog"&&&&&&&&&&&&/activity&&&
&activity android:name=".testDialog"
android:label="这是一个Activity变成的Dialog"
android:theme="@android:style/Theme.Dialog"
&&/activity&好了,你可以运行一下了,如果正常,你将看到和我一样的结果7455人阅读
1。在你的styles.xml文件中可以新建一如下的style:
&&& &style name=&Theme.FloatActivity& parent=&android:style/Theme.Dialog&&
&&& &&& &!-- float_box为我们定义的窗口背景 ,这个不是必须的--&
&&& &&& &item name=&android:windowBackground&&@drawable/float_box&/item&
&&& &/style&
如果窗口要添加窗口背景,可以在drawable中新建一个叫float_box.xml的文件,内容可以如下(自定义):
&?xml version=&1.0& encoding=&utf-8&?&
&shape xmlns:android=&/apk/res/android&&
&&& &solid android:color=&#ffffff& /&
&&& &stroke android:width=&3dp& color=&#000000& /&
&&& &corners android:radius=&3dp& /&
&&& &padding android:left=&10dp& android:top=&10dp& android:right=&10dp& android:bottom=&10dp& /&
2.在AndroidManifest.xml中在你需要显示为窗口的activity中添加如果属性:android:theme=&@style/Theme.FloatActivity& 即可
如果说您觉得新建style.xml及float_box.xml文件的方式麻烦或者只想Test一下,那么可以直接添加您对应需要展示为Dialog style的Activity的android:theme属性值为android:theme=&@android:style/Theme.Dialog&。
&&相关文章推荐
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:70174次
排名:千里之外

我要回帖

更多关于 android activity样式 的文章

 

随机推荐