Android 如何正确扩展 LinearLayout 以创建自定义视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24697405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:36:32  来源:igfitidea点击:

How to Correctly Extend LinearLayout to Create a Custom View

androidandroid-layoutandroid-fragmentsandroid-linearlayoutandroid-context

提问by user1908375

I have some "card", which is a simple LinearLayout with a TextView inside

我有一些“卡片”,它是一个简单的 LinearLayout,里面有一个 TextView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
>
 <TextView
        android:id="@+id/card_label_txt"
        android:layout_width="wrap_content"
        android:text="label" />
</LinearLayout>

then I have my Main Fragment with a vertical LinearLayout.. and in this main fragment I add this "cards" to the main layout:

然后我有一个带有垂直 LinearLayout 的 Main Fragment .. 在这个主要片段中,我将这个“卡片”添加到主布局中:

# main fragment layout
View view = inflater.inflate(R.layout.main_activity, null);
LinearLayout ll = (LinearLayout) view
                .findViewById(R.id.main_activity_ll);
# get card
View card = inflater.inflate(R.layout.card, null);

# add to fragment layout
ll.addView(card);

this works very good AND my card fills the whole width of fragment layout. Actually what I am expecting.

这很好用,我的卡片填满了片段布局的整个宽度。其实是我期待的。

Now I created a separate Class for my Card:

现在我为我的卡创建了一个单独的类:

Class Card extends LinearLayout{

public Card(Context context) {
        super(context);

        View view =  LayoutInflater.from(getContext()).inflate(
                R.layout.card, null);

        this.addView(view);

    }
}

And, then if I add my card to the main fragment layout in the way:

然后,如果我以这种方式将我的卡片添加到主片段布局中:

# main fragment layout
View view = inflater.inflate(R.layout.main_activity, null);
LinearLayout ll = (LinearLayout) view
                .findViewById(R.id.main_activity_ll);

# add new Card to fragment layout
ll.addView(new Card(getActivity());

then it is added BUT the width of the card is no more filled, but wraped to the textview.

然后它被添加但卡片的宽度不再填充,而是包裹到文本视图。

Could someone please explain me why I get different width sizes by this two method of adding same layouts?

有人能解释一下为什么我通过这两种添加相同布局的方法得到不同的宽度大小吗?

Solutionhere is changed Card class that solves this issue:

这里的解决方案是更改了解决此问题的 Card 类:

public Card(Context context) {
       super(context);

       LayoutInflater.from(getContext()).inflate(
                R.layout.card, this);
    }
}

采纳答案by SBerg413

That isn't the correct way to implement a custom View class. In your implementation of the Card class, you're actually creating an additional LinearLayout that is not needed.

这不是实现自定义 View 类的正确方法。在 Card 类的实现中,您实际上是在创建一个不需要的附加 LinearLayout。

First, implement your Card class that extends LinearLayout. Then, reference it in your XML layout like such:

首先,实现扩展 LinearLayout 的 Card 类。然后,在您的 XML 布局中引用它,如下所示:

<com.mypackagename.Card xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
 <TextView
        android:id="@+id/card_label_txt"
        android:layout_width="wrap_content"
        android:text="label" />
</com.mypackagename.Card>

Here's a good tutorial on creating custom views in android.

这是一个关于在 android 中创建自定义视图的好教程。