【问题标题】:Android- including custom linear layout in xmlAndroid-包括xml中的自定义线性布局
【发布时间】:2012-06-15 12:08:32
【问题描述】:

我创建了一个扩展 LinearLayout 的 Java 类,如下所示

    public class News extends LinearLayout{

    Context context;

     public News(Context context) {
    super(context);
     this.context=context;

     ViewFlipper viewFlipper=new ViewFlipper(context);
     viewFlipper.setLayoutParams(this.getLayoutParams());
    this.addView(viewFlipper);

    }

现在是否可以像这样将其包含到 xml 布局中:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res/com.example.android.apis"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">



        <com.example.android.apis.view.LabelView
                android:background="@drawable/green"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" 
                app:text="Green" app:textColor="#ffffffff" />

    </LinearLayout>

【问题讨论】:

  • 是的,有什么问题.....?
  • 谢谢你,因为我找不到任何说明我可以做到这一点的文档......我不知道......

标签: android android-linearlayout android-custom-view


【解决方案1】:
 <com.yourcompanyname.projectname.News<<<change here and check path is proper
                android:background="@drawable/green"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" 
                app:text="Green" app:textColor="#ffffffff" />

【讨论】:

    【解决方案2】:

    是的,但您也应该重写其他构造函数。

    <com.yourproject.package.News
        android:background="@drawable/green"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        app:text="Green" app:textColor="#ffffffff" />
    

    另外,xmlns:app="http://schemas.android.com/apk/res/com.example.android.apis" 也需要更新到您的应用程序包中。

    【讨论】:

    • 是的,我只是以 tat 为例...我明白这一点。但我不明白你所说的覆盖其他构造函数是什么意思……你指的是哪个其他构造函数
    • 我认为您需要不覆盖,而是使用 AttributeSet 实现构造函数,以便它可能将属性传递给 LinearLayout。或者它是可选的,在这种情况下我很抱歉提出这个问题。
    猜你喜欢
    • 1970-01-01
    • 2012-07-01
    • 1970-01-01
    • 2011-04-21
    • 2023-03-14
    • 2012-07-16
    • 2013-01-13
    • 2011-01-23
    • 1970-01-01
    相关资源
    最近更新 更多