【发布时间】:2015-08-20 19:16:51
【问题描述】:
我想创建一个自定义视图,它显示一个带有以下内容的卡片:
- TextView(标题)
- TextView(描述)
- 线性布局(内部布局)
所以我只是扩展了一个 LinearLayout 并用它扩充了我的布局文件:
public class FrageContainerView extends LinearLayout {
private TextView objTextViewCaption;
private TextView objTextViewDescription;
private String caption;
private String description;
private LinearLayout objLayoutInner;
public FrageContainerView(Context context) {
this(context, null);
}
public FrageContainerView(Context context, AttributeSet attrs) {
super(context, attrs);
initialize(context, attrs);
}
public FrageContainerView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize(context, attrs);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public FrageContainerView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initialize(context, attrs);
}
private void initialize(Context context, AttributeSet attrs) {
TypedArray a =
context.obtainStyledAttributes(attrs, R.styleable.options_frageContainerView, 0, 0);
caption = a.getString(R.styleable.options_frageContainerView_caption);
description = a.getString(R.styleable.options_frageContainerView_description);
a.recycle();
setOrientation(LinearLayout.HORIZONTAL);
setGravity(Gravity.CENTER_VERTICAL);
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.view_fragecontainer, this, true);
objLayoutInner = (LinearLayout) findViewById(R.id.linearlayout_inner);
objTextViewCaption = (TextView) findViewById(R.id.textview_caption);
objTextViewDescription = (TextView) findViewById(R.id.textview_description);
objTextViewCaption.setText(caption);
objTextViewDescription.setText(description);
}
使用我的自定义视图的用户应该能够最好在 XML 中添加他自己的组件,如下所示:
<FrageContainerView
android:layout_width="match_parent"
android:layout_height="match_parent"
custom:caption="Hallo"
custom:description="LOLOLOL"
android:background="#FF00FF00">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="sdsdfsdf"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="sdsdfsdf"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="sdsdfsdf"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="sdsdfsdf"/>
</FrageContainerView>
当前状态是,定义的 EditText 在我的自定义视图中膨胀。我希望将示例中的 EditTexts 添加到 InnerLayout,而不是将它们附加到自定义视图。
最好的方法是什么?
【问题讨论】:
-
“我很难解释我想做什么”。是的,您的问题有很多观点,但没有答案,因为它令人困惑和不清楚。解释更多。
-
@Sound Conception 我更新了我的问题:)
-
啊哈!您不是指正常意义上的“用户”(就像使用您的应用程序的人一样)。您实际上是指另一个使用您的课程的软件开发人员,对吧?
-
是的,自定义组件的用户(但可能只有我 :D)
标签: android android-layout android-custom-view