如何在android中创建一个叠加窗口?

时间:2013-11-06 13:06:23

标签: java android

我正在尝试制作一个警告对话框,它会像图片一样显示。我试图使用框架布局,但不能像图片那样使它正确。在我目前的布局中,我使用了imageView和textview。 enter image description here

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ImageView 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:src=""/>
        <TextView 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@strings/text"
        />
    </FrameLayout>
    <!---<other layouts--->
</LinearLayout>

2 个答案:

答案 0 :(得分:1)

您可能需要查看Crouton library

enter image description here enter image description here

答案 1 :(得分:0)

显示带有自定义内容的对话框

ViewGroup myRootViewGroup = /* root ViewGroup of your activity or fragment */;
mContext = /* your activity context */;

AlertDialog.Builder builder = new AlertDialog.Builder (mContext, AlertDialog.THEME_HOLO_DARK);
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.mylayout, (ViewGroup) myRootViewGroup);
builder.setView(layout);
Dialog dialog = builder.create();
dialog.show();

.
.
.
dialog.dismiss();

Creating a custom dialog in Android处也很好的答案。

您可能还认为您要查找的布局基本上是标准的AlertDialog布局(没有按钮),您可以只执行builder.setIcon()和builder.setTitle()。

对于您的布局,更改LinearLayout orientation =“horizo​​ntal”,完全删除FrameLayout,设置ImageView layout_width =“wrap_content”。这样:

<LinearLayout 
android:id="@id/mylayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

 <ImageView 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:src=""/>
 <TextView 
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@strings/text"
/>

</LinearLayout>

您还可能想要在图像/文本视图上使用填充来获得精确的外观。