【问题标题】:How to add padding in drawable如何在drawable中添加填充
【发布时间】:2015-05-13 12:23:47
【问题描述】:

我有自定义的EditTextdrawable:

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

<?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

<?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>

我想在 EditText 中进行填充,以便文本不在 EditText 的最左边。

尝试使用此功能,但不起作用。

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

如何在 EditText drawable 中添加填充?我不想把它放在我的 XML 布局中的每个 EditText 中。

【问题讨论】:

    标签: android android-drawable


    【解决方案1】:

    我找到了这个 .. 希望它会有所帮助。这是逻辑。你需要自定义它。 这是主布局,您可以在其中添加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>
    

    并像这样在你的样式文件中添加一些样式

    <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>
    

    更多详情请点击here

    【讨论】:

      【解决方案2】:

      要在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);
      }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-26
        • 2013-07-12
        • 2022-10-15
        • 2012-02-06
        • 2010-10-14
        • 2012-04-07
        • 2019-02-19
        • 1970-01-01
        相关资源
        最近更新 更多