Android 带布局的自定义视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2407281/
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 05:45:07  来源:igfitidea点击:

custom view with layout

androidlayoutview

提问by user270811

ok,

好的,

what i am trying to do is to embed a custom view in the default layout main.xml:

我想要做的是在默认布局 main.xml 中嵌入自定义视图:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <com.lam.customview.CustomDisplayView
        android:id="@+id/custom_display_view1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />


    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/prev"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="50"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="@string/prev" />
    </LinearLayout>
</LinearLayout>

as you can see the class is called com.lam.customview.CustomDisplayView, with the id of custom_display_view1.

如您所见,该类名为 com.lam.customview.CustomDisplayView,id 为 custom_display_view1。

now in the com.lam.customview.CustomDisplayView class, i want to use another layout called custom_display_view.xml because i don't want to programmatically create controls/widgets.

现在在 com.lam.customview.CustomDisplayView 类中,我想使用另一个名为 custom_display_view.xml 的布局,因为我不想以编程方式创建控件/小部件。

custom_display_view.xml is just a button and an image, the content of which i want to change based on certain conditions:

custom_display_view.xml 只是一个按钮和一个图像,我想根据某些条件更改其内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView 
    android:id="@+id/display_text_view1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    <ImageView 
    android:id="@+id/display_image_view1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">
    </ImageView>

</LinearLayout>

i tried to do:

我试图做:

1)

1)

public CustomDisplayView(Context context, AttributeSet attrs) {
    super(context, attrs);

    try
    {
        // register our interest in hearing about changes to our surface
        SurfaceHolder holder = getHolder();
        holder.addCallback(this);

        View.inflate(context, R.layout.custom_display_view, null);

...

...

but got this error, "03-08 20:33:15.711: ERROR/onCreate(10879): Binary XML file line #8: Error inflating class java.lang.reflect.Constructor ".

但得到这个错误,“03-08 20:33:15.711: ERROR/onCreate(10879): Binary XML file line #8: Error inflating class java.lang.reflect.Constructor”。

2)

2)

public CustomDisplayView(Context context, AttributeSet attrs) {
    super(context, attrs);

    try
    {
        // register our interest in hearing about changes to our surface
        SurfaceHolder holder = getHolder();
        holder.addCallback(this);

        View.inflate(context, R.id.custom_display_view1, null);

...

...

but got this error, "03-08 20:28:47.401: ERROR/CustomDisplayView(10806): Resource ID #0x7f050002 type #0x12 is not valid "

但得到这个错误,“03-08 20:28:47.401: ERROR/CustomDisplayView(10806): Resource ID #0x7f050002 type #0x12 is not valid”

also, if i do it this way, as someone has suggested, it's not clear to me how the custom_display_view.xml is associated with the custom view class.

另外,如果我这样做,正如有人建议的那样,我不清楚 custom_display_view.xml 如何与自定义视图类相关联。

thanks.

谢谢。

回答by Rino Farina

I had the exact same issue, and the problem was that I was using the wrong ID... and it look's like you are too.

我遇到了完全相同的问题,问题是我使用了错误的 ID……看起来你也是。

You should be referencing

你应该参考

R.layout.custom_display_view

R.layout.custom_display_view

-not-

-不是-

R.id.custom_display_view

R.id.custom_display_view

回答by Frank Schwieterman

This blog post helped me understand what to do immensely http://trickyandroid.com/protip-inflating-layout-for-your-custom-view/. In case the blog post disappears, here are some parts of the code:

这篇博文帮助我理解了http://trickyandroid.com/protip-inflating-layout-for-your-custom-view/该怎么做。如果博客文章消失了,这里是代码的一些部分:

public class Card extends RelativeLayout {
    public Card(Context context) {
        super(context);
        init();
    }

    public Card(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public Card(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        inflate(getContext(), R.layout.card, this);
    }
}

with this layout:

使用这种布局:

<?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="wrap_content"
    android:padding="@dimen/card_padding"
    android:background="@color/card_background">

    <ImageView
        ... />

    <TextView
        ... />

</merge>

The control is included like:

该控件包括如下:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin">

    <com.trickyandroid.customview.app.view.Card
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/card_background"
        android:padding="@dimen/card_padding"/>

</FrameLayout>

Where com.trickyandroid.customview.app.view is the namespace of class card. One thing that was new to me was the "merge" tag, which ends up being the same node as the Card tag in the containing doc.

其中 com.trickyandroid.customview.app.view 是类卡的命名空间。对我来说新的一件事是“合并”标签,它最终与包含文档中的 Card 标签相同。

回答by herbertD

You can inflate the cutom_display_view into your custom class by:

您可以通过以下方式将 cutom_display_view 膨胀到您的自定义类中:

public CustomDisplayView(Context context, AttributeSet attrs) {   
    super(context, attrs);           
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if(inflater != null){       
                inflater.inflate(R.layout.custom_display_view, this);
            }
}

回答by Jannie Theunissen

When you inflate your layout, you should pass in a reference to the view instance to identify it as the root. So instead of calling:

当你膨胀你的布局时,你应该传递一个对视图实例的引用,以将它标识为根。所以,而不是调用:

View.inflate(context, R.layout.custom_display_view, null);

Call:

称呼:

View.inflate(context, R.layout.custom_display_view, this);

See: The docs

请参阅:文档

回答by Karan

Try using

尝试使用

context.getLayoutInflater().inflate( R.id.custom_display_view1, null );

回答by Jay

Line #8 is your custom view in the first layout. Are you trying to load the wrong layout, so it's trying to load itself recursively? (I just did the same thing)

第 8 行是第一个布局中的自定义视图。您是否试图加载错误的布局,因此它试图以递归方式加载自己?(我刚刚做了同样的事情)

also you have:

你还有:

 R.layout.custom_display_view

vs.

对比

 R.id.custom_display_view1

with the 1on the end there... which one is it?

1上到底有没有......哪一个呢?