简体   繁体   中英

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:

Custom View Example

(I used different colors to show how the layout is supposed to go)

Here's the 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):

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

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

Add:

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

inside your ActionView constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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