【问题标题】:Need help in adjusting dialog layout在调整对话框布局方面需要帮助
【发布时间】:2016-09-24 10:19:59
【问题描述】:

我想要一个这样的自定义对话框

Desired dialog look

这是我尝试过的

我的dialog, only editText 中没有显示listView

代码

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_add_task:
                AlertDialog.Builder dialog= new AlertDialog.Builder(this);
                LayoutInflater inflater = getLayoutInflater();
                View convertView = (View) inflater.inflate(R.layout.custom_dialog, null);
                dialog.setView(convertView);
                ListView lv = (ListView) convertView.findViewById(R.id.listView1);
                ArrayList<ToDoList> search = new ArrayList<>();
                search.add(0,new ToDoList("")); //populate your list here
                DialogAdapter adapter=new DialogAdapter(this,search,lv);
                lv.setAdapter(adapter);
                dialog.show();
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

custom_dialog

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

</ListView>

dialog_with_edittext

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/img"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:src="@mipmap/add_new_task"
        android:layout_marginLeft="13dp"/>

    <EditText
        android:id="@+id/edtText"
        android:layout_marginLeft="70dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Add a new task"/>

    <ImageView
        android:id="@+id/dateImg"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:src="@mipmap/imgdate"
        android:layout_below="@+id/edtText"
        android:layout_marginLeft="13dp"/>

    <EditText
        android:id="@+id/date"
        android:layout_marginLeft="70dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edtText"
        android:hint="Date"/>

    <ImageView
        android:id="@+id/timeImg"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:src="@mipmap/imgtime"
        android:layout_below="@+id/date"
        android:layout_marginLeft="13dp"/>

    <EditText
        android:id="@+id/time"
        android:layout_marginLeft="70dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/date"
        android:hint="Date"/>

    <Button
        android:id="@+id/cancelBtn"
        android:background="@color/red"
        android:textColor="@color/white"
        android:text="Cancel"
        android:layout_width="165dp"
        android:layout_height="40dp"
        android:layout_below="@+id/time"
        android:layout_marginLeft="13dp"/>

    <Button
        android:id="@+id/okBtn"
        android:background="@color/light_sky_blue"
        android:textColor="@color/white"
        android:text="Save"
        android:layout_width="165dp"
        android:layout_height="40dp"
        android:layout_below="@+id/time"
        android:layout_marginLeft="200dp"/>


</RelativeLayout>

【问题讨论】:

  • 你想调整对话框中的按钮和分隔线消失对吗?
  • 我希望 listview 像我发送的链接一样显示在对话框中。
  • 查看我的答案,然后我已按照您的要求进行编码。
  • @brahmyadigopula 我使用了你的代码,但我的对话框看起来很奇怪

标签: android listview dialog android-edittext


【解决方案1】:
  1. 将 EditText 的背景设置为 android:color/transparent 或白色。这将从EditText 中删除线,以便您可以拥有通用线(分隔线)
  2. 在你想要的任何地方添加这个分隔符

    <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#aa121212" />
    

【讨论】:

  • 你的意思是我使用视图而不是列表视图?
  • 理想情况下,您必须使用列表视图。但你用粗体写了你不想要列表视图。所以这就是解决方案。
  • 我希望在对话框中显示的列表视图与我发送的链接完全相同。
  • 我应该在哪里添加视图?
  • 在每个编辑文本之后。我之前设计了一个类似的布局,使用线性布局作为父级,并结合相对布局和分隔视图作为子级,每个相对布局都包含图像和编辑文本。
【解决方案2】:

dialog_with_edittext.xml 中尝试此代码。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linear"
        android:orientation="vertical">
    <EditText
        android:id="@+id/edtText"
        android:layout_marginLeft="70dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@mipmap/add_new_task"
        android:hint="Add a new task"/>

    <EditText
        android:id="@+id/date"
        android:layout_marginLeft="70dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edtText"
        android:drawableLeft="@mipmap/imgdate"
        android:hint="Date"/>

    <EditText
        android:id="@+id/time"
        android:layout_marginLeft="70dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/date"
        android:drawableLeft="@mipmap/imgtime"
        android:hint="Date"/>
    </LinearLayout>

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_below="@+id/linear"
    android:layout_height="wrap_content">
    <Button
        android:id="@+id/cancelBtn"
        android:background="@color/red"
        android:textColor="@color/white"
        android:text="Cancel"
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="40dp" />

    <Button
        android:id="@+id/okBtn"
        android:background="@color/light_sky_blue"
        android:textColor="@color/white"
        android:text="Save"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="40dp" />

</LinearLayout>
</RelativeLayout>

并使用这个来消失列表视图的分隔线..

android:divider="@null". 

或者您可以将颜色设置为分隔线。

android:divider="#FFCC00"

【讨论】:

  • 我应该在哪里添加android:divider="@null".
猜你喜欢
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 2019-01-25
  • 2023-03-15
  • 2012-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多