Custom LinearLayout, child is not viewing

随声附和 提交于 2019-12-12 02:52:23

问题


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>

回答1:


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

    }



回答2:


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


来源:https://stackoverflow.com/questions/31760844/custom-linearlayout-child-is-not-viewing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!