加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Extending RelativeLayout by inflating from xml?

发布时间:2020-12-16 06:18:40 所属栏目:百科 来源:网络整理
导读:今天需要写一个简答的自定义view,需要在relativelayout里添加几个textview。 按说很简单的需求,折腾半天还是不行。后来直接google+stackoverflow,终于在这里找到了答案。 Extending RelativeLayout by inflating from xml? 问题: I want to create a cla

今天需要写一个简答的自定义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:
第二,填充操作应该长这样:
layoutInflater.inflate(R.layout.tabview_title_subtitle,this);    
在第一步写xml和java文件时,也可以使用RelateLayout,然后类文件中,extends FrameLayout,也可以达到想要的效果。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读