如何在图标和editText下留出长长的一行,并在editText的末尾留出一些空格?

问题描述:

我想有一个对话框看起来类似于此如何在图标和editText下留出长长的一行,并在editText的末尾留出一些空格?

Desired Dialog Look

我试图代码,但该对话框的设计显得怪异。

如何在图标和editText下划一条长条线,并在editText的末尾留下一些 空间?

enter image description here

XML布局

<?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="270dp" 
     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="270dp" 
     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="270dp" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/date" 
     android:hint="Time"/> 

    <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="190dp"/> 


</RelativeLayout> 

代码

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case R.id.action_add_task: 
       AlertDialog.Builder builder = new AlertDialog.Builder(this); 
       LayoutInflater inflater = getLayoutInflater(); 
       builder.setView(inflater.inflate(R.layout.dialog_with_edittext,null)); 
       builder.show(); 
       return true; 

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

绘制线条像这样的东西: http://stackoverflow.com/questions/2658772/vertical-line-using-xml-drawable 至于空间只需添加一些填充到EditTexts – ivan

试试这个XML我改变填充上相对布局,使得它不看起来与怪异该顶部/底部和两侧,还添加了我在评论(Vertical line using XML drawable)中提供的链接,并将按钮居中放置一点,因为它们看起来有点偏离另一个相对视图,并删除了editTexts的下划线

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:paddingTop="8dp" 
    android:paddingBottom="5dp" 
    android:paddingLeft="5dp" 
    android:paddingRight="5dp" 
    android:layout_height="wrap_content"> 


    <ImageView 
     android:id="@+id/img" 
     android:layout_width="35dp" 
     android:layout_height="35dp" 
     android:background="#000" 
     android:layout_marginLeft="13dp"/> 

    <EditText 
     android:layout_marginTop="11dp" 
     android:layout_marginBottom="11dp" 
     android:layout_marginLeft="20dp" 
     android:layout_toRightOf="@id/img" 
     android:background="@android:color/transparent" 
     android:id="@+id/edtText" 
     android:layout_width="270dp" 
     android:layout_height="wrap_content" 
     android:hint="Add a new task"/> 
    <View 
     android:layout_below="@id/edtText" 
     android:layout_width="fill_parent" 
     android:layout_height="1dp" 
     android:background="#000" 
     android:layout_marginTop="-4dp"/> 
    <ImageView 
     android:id="@+id/dateImg" 
     android:layout_width="35dp" 
     android:layout_height="35dp" 
     android:background="#000" 
     android:layout_below="@+id/edtText" 
     android:layout_marginLeft="13dp"/> 

    <EditText 
     android:layout_marginTop="11dp" 
     android:layout_marginBottom="11dp" 
     android:layout_marginLeft="20dp" 
     android:layout_toRightOf="@id/dateImg" 
     android:background="@android:color/transparent" 
     android:id="@+id/date" 
     android:layout_width="270dp" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/edtText" 
     android:hint="Date"/> 
    <View 
     android:layout_below="@id/date" 
     android:layout_width="fill_parent" 
     android:layout_height="1dp" 
     android:background="#000" 
     android:layout_marginTop="-4dp"/> 

    <ImageView 
     android:id="@+id/timeImg" 
     android:layout_width="35dp" 
     android:layout_height="35dp" 
     android:background="#000" 
     android:layout_below="@+id/date" 
     android:layout_marginLeft="13dp"/> 

    <EditText 
     android:layout_marginTop="11dp" 
     android:layout_marginBottom="11dp" 
     android:layout_marginLeft="20dp" 
     android:layout_toRightOf="@id/timeImg" 
     android:background="@android:color/transparent" 
     android:id="@+id/time" 
     android:layout_width="270dp" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/date" 
     android:hint="Time"/> 

    <View 
     android:layout_below="@id/time" 
     android:layout_width="fill_parent" 
     android:layout_height="1dp" 
     android:background="#000" 
     android:layout_marginTop="-4dp"/> 

<RelativeLayout 
    android:layout_centerHorizontal="true" 
    android:layout_below="@+id/time" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <Button 
     android:layout_marginTop="5dp" 
     android:id="@+id/cancelBtn" 
     android:background="#fc2a2a" 
     android:textColor="#ffffff" 
     android:text="Cancel" 
     android:layout_width="165dp" 
     android:layout_height="40dp" 
     android:layout_marginLeft="15dp" 
     android:layout_alignParentLeft="true"/> 

    <Button 
     android:layout_marginTop="5dp" 
     android:id="@+id/okBtn" 
     android:background="#64e3ff" 
     android:textColor="#fff" 
     android:text="Save" 
     android:layout_width="165dp" 
     android:layout_height="40dp" 
     android:layout_marginRight="15dp" 
     android:layout_alignParentRight="true"/> 
</RelativeLayout> 

</RelativeLayout> 

enter image description here

+0

感谢您的答案和所有有用的提示。我尝试使用'android:background =“@ android:color/transparent”'删除editText的underbar行,所有的布局运行 –

+0

Ohh对不起,没有注意到生病立即尝试实现它(/ oo)/ – ivan

+0

完成后,如果您需要任何其他编辑生病,很高兴知道它。 – ivan