1

I have two questions.

1.) Right now I have a popup that has an X displaying inside on the top right, but I can't get it to appear overlaying the top right corner of the popup. What can I do to achieve the desired "X" button?

currently: http://i.imgur.com/zKN0cWv.png

desired: http://i.imgur.com/Lj5gHhY.png

2.) My "Top Button" and "Bottom Button" fill the entire width of the screen even though I have it to wrap content. Is there a way to make the buttons be narrower? (Actually, I would like to have them both be narrower, but still the same width even if one button's text is longer than the other). Does it have something to do with the way I laid out the XML? Ideally, I want to have a background image on the popup, but when I put in the image, the image goes inside the 3 textviews too, which leads me to think that I don't have a very good XML file. Does anyone know how to solve this problem too? Thanks!

layout/popup.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dialog"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

<TextView
    android:id="@+id/popup_header"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="15dp"
    android:text="@string/popup_message"
    android:textSize="20sp"
    android:textStyle="bold"
    android:textColor="#464646" >
</TextView>

<TextView
    android:id="@+id/popup_text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/popup_header"
    android:gravity="center_vertical|center_horizontal"
    android:text="@string/popup_message2"
    android:textSize="20sp"
    android:textColor="#464646" >
</TextView>

<TextView
    android:id="@+id/popup_text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/popup_text1"
    android:text="@string/popup_message3"
    android:textSize="20sp"
    android:textStyle="bold"
    android:textColor="#464646" >
</TextView>

<Button
    android:id="@+id/top_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/popup_text2"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="12dp"
    android:background="#f000ff"
    android:text="@string/top_button"
    android:textColor="#ffffff"
    android:textSize="20sp" >
</Button>

<Button
    android:id="@+id/bottom_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/top_button"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="5dp"
    android:background="#f000ff"
    android:text="@string/bottom_button"
    android:textColor="#ffffff"
    android:textSize="20sp" >
</Button>

<Button
    android:layout_alignParentRight="true" android:text="@string/dismiss"
    android:textColor="#FFF" android:background="@drawable/round_button_background"
    android:gravity="center_vertical|center_horizontal"
    android:layout_margin="5dp" android:layout_height="25dp"
    android:layout_width="25dp" android:textSize="12sp" android:textStyle="bold"
    android:onClick="cancelActivity" >
</Button>

</RelativeLayout>

drawable/round_button_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval">
    <solid android:color="#9F2200" />
    <stroke android:width="3dp" android:color="#FFF" />
</shape>

values/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
  <style name="Theme.CustomDialog"     parent="@android:style/Theme.Translucent.NoTitleBar.Fullscreen">
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:padding">4dp</item>
    <item name="android:background">#f8adfc</item>
  </style>
</resources>
4

2 回答 2

0

第一个问题的解答:

正如你想在左上角显示图像,建议你提供的链接为此首先你为根布局创建一个 id,即当你在那里声明对话框时的相对布局,现在这样的代码

<ImageView
        android:id="@+id/close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/dialog"
        android:background="@drawable/close" />

就第二个问题而言,我可以看到您声明的按钮在那里有一个宽度匹配的父级。首先将其设置为所有按钮的 wrap_contect。如果这对您没有帮助,请在 java 代码本身中动态地为弹出窗口提供静态宽度

于 2013-11-06T11:10:48.993 回答
0
// try this
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <RelativeLayout
        android:id="@+id/dialog"
        android:layout_width="wrap_content"
        android:layout_marginTop="7dp"
        android:layout_marginLeft="7dp"
        android:background="@android:color/white"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/popup_header"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="15dp"
            android:text="@string/popup_message"
            android:textSize="20sp"
            android:textStyle="bold"
            android:textColor="#464646" >
        </TextView>

        <TextView
            android:id="@+id/popup_text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_below="@+id/popup_header"
            android:gravity="center_vertical|center_horizontal"
            android:text="@string/popup_message2"
            android:textSize="20sp"
            android:textColor="#464646" >
        </TextView>

        <TextView
            android:id="@+id/popup_text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_below="@+id/popup_text1"
            android:text="@string/popup_message3"
            android:textSize="20sp"
            android:textStyle="bold"
            android:textColor="#464646" >
        </TextView>

        <Button
            android:id="@+id/top_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/popup_text2"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="12dp"
            android:background="#f000ff"
            android:text="@string/top_button"
            android:textColor="#ffffff"
            android:textSize="20sp" >
        </Button>

        <Button
            android:id="@+id/bottom_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/top_button"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="5dp"
            android:background="#f000ff"
            android:text="@string/bottom_button"
            android:textColor="#ffffff"
            android:textSize="20sp" />



    </RelativeLayout>
    <Button
        android:textColor="#FFFFFF"
        android:background="@drawable/round_button_background"
        android:layout_height="25dp"
        android:layout_width="25dp"
        android:textSize="12sp"
        android:textStyle="bold"
        android:onClick="cancelActivity"/>
</FrameLayout>
于 2013-11-06T11:14:30.353 回答