带有元素的自定义 android 控件
Custom android controls with elements
我对创建自定义 android 控件有一个非常基本的想法和经验。
我知道我可以创建一个自定义控件,它基本上是一个包含多个控件的布局,添加属性和我想要的任何类型的逻辑。
但现在我需要一些不同的东西。我有一个应用程序,其中所有 EditText
元素下方都有一条灰色水平线。
我知道这可以通过 backgroundTint
实现,但这仅适用于 api 21 及以上。
因此,我不想在我使用的每个 EditText
元素下方添加这条灰线,而是想创建一个扩展 EditText
但也有这条灰线的自定义元素。像这样:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:src="@drawable/gray_divider"/>
</LinearLayout>
后面的代码中:
public class MyEditText extends LinearLayout {
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
Init();
}
private void Init() {
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.my_edit_text, this);
InitViews(view);
}
所以我的问题是:
为了让 MyEditText
像 EditText
一样工作,我必须为每个 EditText
属性创建一个属性。我想这样做:
<MyEditText android:layout_width="match_parent"
android:layout_height="match_parent"
android:text_color="@android:color/white" />
不在后面的代码中添加任何内容。这可能吗?
在这种情况下,最好使用带底线的背景。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="-10dp"
android:right="-10dp"
android:top="-10dp">
<shape android:shape="rectangle">
<gradient
android:angle="270"
android:endColor="@color/transparent"
android:startColor="@color/transparent" />
<stroke
android:width="@dimen/border"
android:color="@color/border" />
<corners android:radius="@dimen/border" />
</shape>
</item>
制作一个.xml资源并放入Drawablefolder.Egborder_bottom.xml
对于编辑文本,只需提供
这样的背景
android:background="@drawable/border_bottom"
希望对您有所帮助。
我对创建自定义 android 控件有一个非常基本的想法和经验。
我知道我可以创建一个自定义控件,它基本上是一个包含多个控件的布局,添加属性和我想要的任何类型的逻辑。
但现在我需要一些不同的东西。我有一个应用程序,其中所有 EditText
元素下方都有一条灰色水平线。
我知道这可以通过 backgroundTint
实现,但这仅适用于 api 21 及以上。
因此,我不想在我使用的每个 EditText
元素下方添加这条灰线,而是想创建一个扩展 EditText
但也有这条灰线的自定义元素。像这样:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:src="@drawable/gray_divider"/>
</LinearLayout>
后面的代码中:
public class MyEditText extends LinearLayout {
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
Init();
}
private void Init() {
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.my_edit_text, this);
InitViews(view);
}
所以我的问题是:
为了让 MyEditText
像 EditText
一样工作,我必须为每个 EditText
属性创建一个属性。我想这样做:
<MyEditText android:layout_width="match_parent"
android:layout_height="match_parent"
android:text_color="@android:color/white" />
不在后面的代码中添加任何内容。这可能吗?
在这种情况下,最好使用带底线的背景。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="-10dp"
android:right="-10dp"
android:top="-10dp">
<shape android:shape="rectangle">
<gradient
android:angle="270"
android:endColor="@color/transparent"
android:startColor="@color/transparent" />
<stroke
android:width="@dimen/border"
android:color="@color/border" />
<corners android:radius="@dimen/border" />
</shape>
</item>
制作一个.xml资源并放入Drawablefolder.Egborder_bottom.xml
对于编辑文本,只需提供
这样的背景 android:background="@drawable/border_bottom"
希望对您有所帮助。