3
我正在尝试为android创建自定义布局。它通常在屏幕上绘制,但没有内部视图。仅绘制我的group_box.xml。我如何从我的自定义布局访问内部视图(TextView与ID测试)或如何绘制它们?基于已有布局的自定义布局
main.xml中
<my.example.GroupBox
android:layout_width="match_parent"
android:layout_height="40sp">
<TextView android:text="TEST"
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</my.example.GroupBox>
group_box.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/groupbox">
<LinearLayout style="@style/groupboxContent"
android:id="@+id/content"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
<TextView style="@style/groupboxLabel"
android:id="@+id/caption"
android:text="@string/visit"/>
</RelativeLayout>
GroupBox.java
public class GroupBox extends LinearLayout {
public GroupBox(Context context) {
super(context);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.group_box, this);
}
public GroupBox(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.group_box, this);
}
}
你不完全理解我。我想在内使用id test中的 TextView设置视图到LinearLayout中,并在group_box.xml中使用id内容,并且希望通过xml而不是像代码中的代码那样通过标题来完成。谢谢 –
2011-04-21 07:58:10
确定sry。您不能将此视图放在中,并在GroupBox类中访问它。您只能从视图或者使main.xml膨胀的活动访问。想一想:您可以将添加到其他布局。如果您在GroupBox中访问您未添加到XML的视图,您将得到NullPointerException。 –
passsy
2011-04-21 08:02:31
但Android如何在LinearLayout或其他布局中执行此操作? – 2011-04-21 08:05:14