简体   繁体   English

如何在drawable中添加填充

[英]How to add padding in drawable

I have custom EditText drawable: 我有自定义EditText drawable:

edit_text_selector edit_text_selector

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_focused="true" android:drawable="@drawable/edit_text_background_on" />
    <item android:state_focused="false" android:drawable="@drawable/edit_text_background_off" />

</selector>

edit_text_background_on edit_text_background_on

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape>
            <solid android:color="@color/main_color" />
        </shape>
    </item>

    <item
        android:bottom="1.5dp"
        android:left="1.5dp"
        android:right="1.5dp">
        <shape>
            <solid android:color="@color/background_color" />
        </shape>
    </item>

    <item 
        android:bottom="5.0dp" >
        <shape>
            <solid android:color="@color/background_color" />
        </shape>
    </item>

</layer-list>

edit_text_background_off edit_text_background_off

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape>
            <solid android:color="@color/edittext_off_color" />
        </shape>
    </item>

    <item
        android:bottom="1.5dp"
        android:left="1.5dp"
        android:right="1.5dp">
        <shape>
            <solid android:color="@color/background_color" />
        </shape>
    </item>

    <item android:bottom="5.0dp">
        <shape>
            <solid android:color="@color/background_color" />
        </shape>
    </item>

</layer-list>

I want to have padding in EditText so that the text is not in the most left position of EditText. 我想在EditText中使用填充,以使文本不在EditText的最左侧。

Tried to use this but doesn't work. 尝试使用此功能,但不起作用。

<inset android:insetLeft="6dip"/>

How can I add padding in EditText drawable? 如何在EditText drawable中添加填充? I don't want to put it in every EditText in my XML layout. 我不想将其放在XML布局的每个EditText中。

For adding the padding in EditText,you need to create the custom editText, override the onLayout() method and set the padding in that method such as 要在EditText中添加填充,您需要创建自定义editText,覆盖onLayout()方法并在该方法中设置填充,例如

public class CustomEditText extends EditText
{
  public CustomEditText(Context context) 
  {
     super(context);
  }

  public CustomEditText(Context context,AttributeSet attrs) 
  {
     super(context, attrs);
  }

  public CustomEditText(Context context,AttributeSet  attrs, int defStyle) 
   {
     super(context, attrs, defStyle);
   }

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    // set the padding here
    this.setPadding(20,20,20, 20);
    super.onLayout(changed, left, top, right, bottom);
}
}

I found this .. Hope it will help. 我发现了这个。希望对您有所帮助。 This is the logic . 这是逻辑。 you need to customize it. 您需要对其进行自定义。 This is main Layout where you can add edittext 这是主布局,您可以在其中添加edittext

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#eee">

    <!-- Input Group -->
    <EditText style="@style/Widget.Group.Top" />
    <EditText style="@style/Widget.Group" />
    <EditText style="@style/Widget.Group.Bottom" />

    <!-- Single item -->
    <EditText style="@style/Widget.Group.Single" />

</LinearLayout>

and add some style in you style file like this 然后在样式文件中添加一些样式

<style name="Widget.Group" parent="@android:style/Widget">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">46dp</item>
        <item name="android:layout_marginLeft">10dp</item>
        <item name="android:layout_marginRight">10dp</item>
        <item name="android:layout_marginTop">-1dp</item> <!-- Ensures we don't get a 2 dp top stroke -->
        <item name="android:padding">4dp</item>
        <!--<item name="android:background">@drawable/bg_input_group</item>-->
    </style>

    <style name="Widget.Group.Top">
        <item name="android:layout_marginTop">10dp</item>
        <!--<item name="android:background">@drawable/bg_input_group_top</item>-->
    </style>

    <style name="Widget.Group.Bottom">

        <item name="android:layout_marginTop">20dp</item>
        <!--<item name="android:background">@drawable/bg_input_group_bottom</item>-->
    </style>

    <style name="Widget.Group.Single" parent="Widget.Group.Top">
        <!--<item name="android:background">@drawable/bg_input_group_single</item>-->
    </style>

For more details click here . 有关更多详细信息,请单击此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM