简体   繁体   English

Android-如何布局和使用自定义视图?

[英]Android - How to layout and use a custom View?

I am writing an app on Android Studio and I am trying to create a custom view with a specific form I need, something like: 我正在Android Studio上编写应用程序,并且尝试使用所需的特定表单创建自定义视图,例如:

Custom View Example 自定义视图示例

(I used different colors to show how the layout is supposed to go) (我使用了不同的颜色来显示布局应该如何进行)

Here's the xml: 这是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>

The problem comes when I try to add this view to another layout in the app.. It comes as a blank square. 当我尝试将此视图添加到应用程序中的另一个布局时,问题就来了。它以空白方块的形式出现。 I have been trying to look for an answer on how to properly do this, but nothing helps. 我一直在寻找有关如何正确执行此操作的答案,但是没有任何帮助。

This is what I have in the xml of the activity where I want the view (ActionView): 这是我想要视图(ActionView)的活动xml中的内容:

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

Here's the ActionView's constructors I have (not sure what to do with them to be honest, just copied them from a tutorial): 这是我拥有的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);
}

I think you're missing the most important part. 我认为您错过了最重要的部分。 You're not tying your layout to your ActionView object... 您没有将布局绑定到ActionView对象...

Add: 加:

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

inside your ActionView constructor. 在您的ActionView构造函数中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM