简体   繁体   中英

Custom LinearLayout, child is not viewing

I'm creating my Cell view by extending LinearLayout, it's creating parent, but not showing children. I really couldn't find the problem?

Cell cell = new Cell(ctx);
cell.setLetterAndPosition(new Point(1,1), new Letter("A");

import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import pe.kor.kelime.Model.Letter;
import pe.kor.kelime.R;

/**
 * Created by me on 7/29/15.
 */
public class Cell extends LinearLayout {

    private Letter letter;
    private Point position; /* 0-3 / 0-3 based position*/

    private TextView text;

    private boolean touched = false;

    public Cell(Context context) {
        super(context);
        init(context);
    }

    public Cell(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

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

    public void setLetterAndPosition(Point position, Letter letter) {
        this.letter = letter;
        this.position = position;
        this.text.setText(letter.getLetter());
    }

    void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.view_cell, this);
        text = (TextView) this.findViewById(R.id.text);
        this.setBackgroundColor(Color.parseColor("#ffffff"));
    }

    public Letter getLetterObject() {
        return letter;
    }

    public void setTouched(boolean e) {
        this.touched = e;
        if(this.touched) {
            this.setBackgroundColor(Color.parseColor("#ff0000"));
        }
        else {
            this.setBackgroundColor(Color.parseColor("#ffffff"));
        }
    }

    public boolean isTouched() {
        return touched;
    }

    public Point getPositionInBoard() {
        return position;
    }
}

View_cell.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5.5dp"
    android:clickable="true">

    <TextView
        android:id="@+id/text"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:background="#ff0369"
        android:baselineAligned="false"
        android:clickable="true"
        android:gravity="center|center_vertical"
        android:includeFontPadding="false"
        android:text="A"
        android:textColor="#000"
        android:textSize="36sp"
        android:textStyle="bold"/>

</LinearLayout>

I had to override onLayout method.

onLayout(boolean paramBoolean, int left, int top, int right, int bottom)

    @Override
    protected void onLayout(boolean paramBoolean, int left, int top, int right, int bottom)
    {

        int widthOfCell = right - left;

        getChildAt(0)
                .layout(1,
                        1,
                        widthOfCell,
                        widthOfCell
                );

    }

I think that the problem is your Textview. You have not added the textview to your linearlayout. Change your method init

void init(Context context) {
    LayoutInflater.from(context).inflate(R.layout.view_cell, this);
    text = (TextView) this.findViewById(R.id.text);
    this.addView(text);
    this.setBackgroundColor(Color.parseColor("#ffffff"));
}

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