对话框无法正常工作

时间:2016-02-05 10:21:00

标签: android dialog

我想实现以下设计:

Screen Shot

单击imageview时会显示这五个选项。我正在使用对话框实现此设计。

1.moreOption.xml

    <?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="match_parent"
    android:orientation="vertical"
    android:weightSum="1">

    <TextView
        android:id="@+id/viewContacts"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight=".2"
        android:gravity="center_vertical"
        android:paddingLeft="@dimen/margin10"
        android:text="View Contacts"
        android:textSize="20sp" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:background="#c0c0c0" />

    <TextView
        android:id="@+id/archiveChat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight=".2"
        android:gravity="center_vertical"
        android:paddingLeft="@dimen/margin10"
        android:text="Archive Chat"
        android:textSize="20sp" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:background="#c0c0c0" />

    <TextView
        android:id="@+id/deleteChat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight=".2"
        android:gravity="center_vertical"
        android:paddingLeft="@dimen/margin10"
        android:text="Delete Chat"
        android:textSize="20sp" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:background="#c0c0c0" />

    <TextView
        android:id="@+id/markAsUnread"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight=".2"
        android:gravity="center_vertical"
        android:paddingLeft="@dimen/margin10"
        android:text="Mark as unread"
        android:textSize="20sp" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:background="#c0c0c0" />

    <TextView
        android:id="@+id/emailChat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight=".2"
        android:gravity="center_vertical"
        android:paddingLeft="@dimen/margin10"
        android:text="Email Chat"
        android:textSize="20sp" />
</LinearLayout>

下面是单击imageview时显示对话框的代码。

   ImageView imgMoreOption = (ImageView) view.findViewById(R.id.imgMoreoption);

   imgMoreOption.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {
           Dialog dialog = new Dialog(context);
           dialog.setContentView(R.layout.more_option);
           dialog.show();
       }
   });

现在点击imageview,我将收到以下屏幕截图。

screenshot

但是使用对话框,我没有按照从顶部获得的设计获得更多的空间。请指导我如何实现给定的设计。

已编辑的代码

我已修复此问题。实际上对话框显示空标题。我已更改下面给出的代码:

imgMoreOption.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Dialog dialog = new Dialog(context);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // hide the title bar
            dialog.setContentView(R.layout.more_option);
            dialog.show();
        }
    });

moreOption.xml

    <?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="match_parent"
    android:orientation="vertical"
    android:weightSum="1">

    <TextView
        android:id="@+id/viewContacts"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/margin10"
        android:layout_weight=".2"
        android:gravity="center_vertical"
        android:paddingLeft="@dimen/margin10"
        android:text="View Contacts"
        android:textSize="20sp" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_marginTop="@dimen/margin10"
        android:background="#c0c0c0" />

    <TextView
        android:id="@+id/archiveChat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/margin10"
        android:layout_weight=".2"
        android:gravity="center_vertical"
        android:paddingLeft="@dimen/margin10"
        android:text="Archive Chat"
        android:textSize="20sp" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_marginTop="@dimen/margin10"
        android:background="#c0c0c0" />

    <TextView
        android:id="@+id/deleteChat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/margin10"
        android:layout_weight=".2"
        android:gravity="center_vertical"
        android:paddingLeft="@dimen/margin10"
        android:text="Delete Chat"
        android:textSize="20sp" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_marginTop="@dimen/margin10"
        android:background="#c0c0c0" />

    <TextView
        android:id="@+id/markAsUnread"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/margin10"
        android:layout_weight=".2"
        android:gravity="center_vertical"
        android:paddingLeft="@dimen/margin10"
        android:text="Mark as unread"
        android:textSize="20sp" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_marginTop="@dimen/margin10"
        android:background="#c0c0c0" />

    <TextView
        android:id="@+id/emailChat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/margin10"
        android:layout_marginTop="@dimen/margin10"
        android:layout_weight=".2"
        android:gravity="center_vertical"
        android:paddingLeft="@dimen/margin10"
        android:text="Email Chat"
        android:textSize="20sp" />


</LinearLayout>

3 个答案:

答案 0 :(得分:1)

您应该删除Dialog标题栏。

使用

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

对于TextView你可以使用android预定义样式之一或自定义样式

将此内容添加到xml文件中的TextView

style="?android:attr/listSeparatorTextViewStyle"

答案 1 :(得分:1)

1)当您使用weight时,如果您将android:layout_height="wrap_content"中的android:layout_height="0dp"更改为TextView,则会更好。

2)添加requestWindowFeature(Window.FEATURE_NO_TITLE);以删除标题栏/布局(空白空间)

@Override
public void onClick(View view) {
    Dialog dialog = new Dialog(context);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.more_option);
    dialog.show();
}

PS: 如果您使用LinearLayout的固定高度TextView固定高度,则会更好1}} S上。

答案 2 :(得分:0)

你可以这样做:

<?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main"
    tools:context="com.pathfinder.myapplication.MainActivity">
    <TextView
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:id="@+id/viewContacts"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight=".2"
        android:gravity="center_vertical"
        android:paddingLeft="16dp"
        android:text="View Contacts"
        android:textSize="20sp" />

    <View
        android:id="@+id/view1"
        android:layout_below="@+id/viewContacts"
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:background="#c0c0c0" />

    <TextView
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:layout_below="@+id/view1"
        android:id="@+id/archiveChat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight=".2"
        android:gravity="center_vertical"
        android:paddingLeft="16dp"
        android:text="Archive Chat"
        android:textSize="20sp" />

    <View
        android:id="@+id/view2"
        android:layout_below="@+id/archiveChat"
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:background="#c0c0c0" />

    <TextView
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:layout_below="@+id/view2"
        android:id="@+id/deleteChat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight=".2"
        android:gravity="center_vertical"
        android:paddingLeft="16dp"
        android:text="Delete Chat"
        android:textSize="20sp" />

    <View
        android:id="@+id/view3"
        android:layout_below="@id/deleteChat"
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:background="#c0c0c0" />

    <TextView
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:layout_below="@id/view3"
        android:id="@+id/markAsUnread"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight=".2"
        android:gravity="center_vertical"
        android:paddingLeft="16dp"
        android:text="Mark as unread"
        android:textSize="20sp" />

    <View
        android:id="@+id/view4"
        android:layout_below="@+id/markAsUnread"
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:background="#c0c0c0" />

    <TextView
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:layout_below="@id/view4"
        android:id="@+id/emailChat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight=".2"
        android:gravity="center_vertical"
        android:paddingLeft="16dp"
        android:text="Email Chat"
        android:textSize="20sp" />

</RelativeLayout>
相关问题