如何在drawable中添加填充

时间:2015-05-13 02:49:03

标签: android android-drawable

我有自定义#include <stdio.h> #include <stdlib.h> #include <string.h> FILE *fr; char *line; char *word; size_t len =256; int i=0; int sum=0; char *vali; const char delim = ' '; int flag=0; int main(int argc, char* argv[]){ line = (char *)malloc(len); word = (char *)malloc(len); /* line = (char *)malloc(sizeof(&len)); word = (char *)malloc(sizeof(&len)); vali = (char *)malloc(sizeof(&len)); */ fr = fopen(argv[1], "r"); if(fr==NULL){ //fr="/dev/stdin"; <-- Piece of code I need for this stackoverflow question } while (getline(&line, &len, fr) != -1){ /* printf("%s", line ); */ if(strlen(line) != 1){ sscanf(line,"%s%*[^\n]",word); printf("%-10s ", word); char *scores = line + strlen(word) + 1; /* printf("scores: %s", scores); */ vali=strtok(scores, &delim); while(vali != NULL){ sum=sum+atoi(vali); vali = strtok(NULL, &delim); } printf("%3d\n", sum); sum=0; } } fclose(fr); free(line); // free(word); // free(vali); return 0; } drawable:

edit_text_selector

EditText

edit_text_background_on

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

我想在EditText中填充,以便文本不在EditText的最左侧位置。

试图使用它,但没有工作。

<?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 drawable中添加填充?我不想把它放在我的XML布局中的每个EditText中。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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