【问题标题】:Android - How to layout and use a custom View?Android - 如何布局和使用自定义视图?
【发布时间】:2015-02-03 23:28:27
【问题描述】:

我正在 Android Studio 上编写一个应用程序,我正在尝试使用我需要的特定表单创建一个自定义视图,例如:

Custom View Example

(我使用不同的颜色来显示布局应该如何进行)

这是xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="150dp"
    android:weightSum="2"
    android:background="@android:color/holo_red_dark">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="1"
        android:paddingLeft="15dp"
        android:paddingRight="15dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="75dp"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/hourLbl"
            android:layout_gravity="center"
            android:background="@android:color/holo_blue_bright"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="75dp"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/minuteLbl"
            android:layout_gravity="center"
            android:background="@android:color/holo_orange_dark"/>

    </LinearLayout>

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/actionName"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@android:color/darker_gray"/>

</LinearLayout>

当我尝试将此视图添加到应用程序中的另一个布局时,问题就出现了。它是一个空白方块。我一直在尝试寻找有关如何正确执行此操作的答案,但没有任何帮助。

这是我想要查看的活动的 xml 中的内容 (ActionView):

<RelativeLayout 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" tools:context=".HomeActivity">

    <...ActionView
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:id="@+id/firstAction" />

</RelativeLayout>

这是我拥有的 ActionView 的构造函数(老实说,不确定如何处理它们,只是从教程中复制它们):

public ActionView(Context context) {
    super(context);
}

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

    TypedArray a = context.getTheme().obtainStyledAttributes(
            attrs,
            R.styleable.ActionView,
            0, 0);

    try {
        mHourLbl = a.getString(R.styleable.ActionView_hourLbl);
        mMinuteLbl = a.getString(R.styleable.ActionView_minuteLbl);
        mActionName = a.getString(R.styleable.ActionView_actionName);
    } finally {
        a.recycle();
    }

}

public ActionView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

【问题讨论】:

  • 你对ActionView的构造函数的实现是什么?问题可能就在那里
  • @blipinsk 你有它。

标签: android android-layout android-studio android-custom-view android-inflate


【解决方案1】:

我认为你错过了最重要的部分。您没有将您的布局绑定到您的 ActionView 对象...

添加:

LayoutInflater.from(context).inflate(R.layout.<xml_of_action_view>, this, true);

在您的 ActionView 构造函数中。

【讨论】:

  • 这听起来很像答案,但第二个参数应该是 ViewGroup 而“this”是视图的子类,不知道在这里做什么。
  • 它应该是LinearLayout 的子类,因为您的&lt;xml_of_action_view&gt; 布局xml 根对象是LinearLayout
  • 完美!将 super 从视图更改为 LinearLayout 并且它起作用了!谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-16
  • 2013-11-06
  • 2015-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-24
相关资源
最近更新 更多