【问题标题】:Custom LinearLayout, child is not viewing自定义线性布局,孩子没有查看
【发布时间】:2015-08-01 10:30:23
【问题描述】:

我正在通过扩展 LinearLayout 创建我的单元格视图,它正在创建父级,但 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>

【问题讨论】:

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


【解决方案1】:

我必须重写 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
                );

    }

【讨论】:

    【解决方案2】:

    我认为问题出在您的 Textview 上。您尚未将 textview 添加到您的线性布局中。 改变你的方法初始化

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-09
      • 1970-01-01
      相关资源
      最近更新 更多