自定义LinearLayout,孩子没有查看

时间:2015-08-01 10:30:23

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

我通过扩展LinearLayout来创建我的Cell视图,它创建父级,但not showing个孩子。我真的无法找到问题吗?

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>

2 个答案:

答案 0 :(得分:0)

我必须覆盖onLayout方法。

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

    }

答案 1 :(得分:0)

我认为问题是你的Textview。您尚未将textview添加到linearlayout。 更改方法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"));
}