Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

android - How to customize the width and height when show an Activity as a Dialog

If I have defined a Activity:

public class DialogActivity extends Activity{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(dialog_activity.xml);
    }


}

I would like to display the above activity like a dialog, so in the AndroidManifest.xml file, I declare this activity like below:

<activity android:name=".DialogActivity" android:theme="@android:style/Theme.Dialog"/>

Everything is fine at this point, my DialogActivity showed as a dialog.

The problem is How to customize the width and height of the DialogActivity to make it more like a small dialog? (Currently it occupies most of the screen by default)

----------------------------Update-----------------------

I defined a custom theme like below:

<style name="myDialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>

        <item name="android:width">100dip</item> 
        <item name="android:height">100dip</item>

</style>

Then, declare the DialogActivity in AndroidManifest.xml as.

<activity android:name=".DialogActivity" android:theme="@style/myDialog"/>

My DialogActivity now even occupies the whole screen:( . The width and height definition:

<item name="android:width">100dip</item>

in the theme do not take any effect, why?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I finally figured out one way to customize the size of my DialogActivity.

That's inside the onCreate() method of DialogActivity, add the following code:

WindowManager.LayoutParams params = getWindow().getAttributes();  
params.x = -20;  
params.height = 100;  
params.width = 550;  
params.y = -10;  

this.getWindow().setAttributes(params); 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...