自定义LinearLayout,子级未查看 [英] Custom LinearLayout, child is not viewing

查看:111
本文介绍了自定义LinearLayout,子级未查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过扩展LinearLayout创建我的Cell视图,它创建了父级,但是创建了not showing个子级.我真的找不到问题了吗?

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

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>

推荐答案

我认为问题出在您的Textview.您尚未将textview添加到linearlayout中. 更改您的方法init

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

这篇关于自定义LinearLayout,子级未查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆