今天需要写一个简答的自定义view,需要在relativelayout里添加几个textview。
按说很简单的需求,折腾半天还是不行。后来直接google+stackoverflow,终于在这里找到了答案。
Extending RelativeLayout by inflating from xml?
问题:
I want to create a class named TabView which extends RelativeLayout and it is inflated from xml that contains RelativeLayout. The thing is that it doesn't seem to work as I expected. Did I do something wrong in the constructor? I know layoutInflater.inflate returns View object but what do I have to make it equal to? Any advice is apprecited!
我想创建一个类名为TabView的ViewGroup,它扩展自RelativeLayout。它是有包含RelativeLayout的xmf填充的。问题是它看起来不像我想的那样工作。我在构造函数里做了什么误操作么?我知道layoutinflater.inflate返回View对象,但是我应该怎么做呢?任何建议都可以
private class TabView extends RelativeLayout { private int mIndex; public TabView(Context context) { super(context,null,R.attr.vpiTabPageIndicatorStyle); LayoutInflater layoutInflater = LayoutInflater.from(context); layoutInflater.inflate(R.layout.tabview_title_subtitle,null); } public int getIndex() { return mIndex; } public void setTitle() { TextView title = (TextView)findViewById(R.id.title); title.setText("Followers"); } public void setSubitle() { TextView subtitle = (TextView)findViewById(R.id.subtitle); subtitle.setText("435"); } }The following is tabview_title_subtitle.xml
以下是tabview_title_subtitle.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:text="Title" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/subtitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_below="@+id/title" android:text="Subtitle" android:textAppearance="?android:attr/textAppearanceMedium" /> </RelativeLayout>
First,your layout should look like this:
首先,你的布局应该长这样:
<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:text="Title" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/subtitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_below="@+id/title" android:text="Subtitle" android:textAppearance="?android:attr/textAppearanceMedium" /> </merge>In other way you'll end up with
RelativeLayout
that contains another
RelativeLayout
你不应当在一个RelativeLayout里包含另一个RelativeLayout。
Second. Inflate it like this:
Second. Inflate it like this:
第二,填充操作应该长这样:
原文链接:https://www.f2er.com/xml/296696.html