【问题标题】:custom view with layout带有布局的自定义视图
【发布时间】:2011-01-25 07:45:41
【问题描述】:

好的,

我要做的是在默认布局 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>

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

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

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>

我尝试过:

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);

...

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

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);

...

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

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

谢谢。

【问题讨论】:

  • 你好我也有同样的问题...你有什么解决办法吗???

标签: android layout view


【解决方案1】:

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

你应该引用

R.layout.custom_display_view

-不是-

R.id.custom_display_view

【讨论】:

  • 完美答案,我错误地遇到了同样的问题,这真的很有帮助
【解决方案2】:

这篇博文极大地帮助了我了解该做什么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);
    }
}

采用这种布局:

<?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>

控件包含如下:

<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>

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

【讨论】:

    【解决方案3】:

    您可以通过以下方式将 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);
                }
    }
    

    【讨论】:

    • 这更干净:View.inflate(context, R.layout.myactionbar, this);
    【解决方案4】:

    当你扩展你的布局时,你应该传入一个对视图实例的引用,以将它标识为根。所以不要打电话:

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

    呼叫:

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

    见:The docs

    【讨论】:

      【解决方案5】:

      第 8 行是您在第一个布局中的自定义视图。你是否试图加载错误的布局,所以它试图递归地加载自己? (我只是做了同样的事情)

      你也有:

       R.layout.custom_display_view
      

      对比

       R.id.custom_display_view1
      

      最后有1...是哪一个?

      【讨论】:

        【解决方案6】:

        尝试使用

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

        【讨论】:

          猜你喜欢
          • 2011-07-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-21
          • 1970-01-01
          • 1970-01-01
          • 2011-03-16
          相关资源
          最近更新 更多