마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

정사각형 모양의 textview를 만드는 방법

0 추천

안녕하세요. 연습삼아 스도쿠어플 만들어보려는 초보자 입니다.

아래 이미지처럼 만들어보려고 하는데요. 

문제는 textview를 어떻게 정사각형으로 만드냐 입니다.

구글링해서 찾은 방법은...

새로운 자바클래스를 생성해서, TextView를 상속시킨 다음, 

onMeasure 메소드를 이용해 텍스트뷰의 높이를 너비랑 똑같이 만들어주는 방법이었습니다.

따라서, SquareTextView라는 새로운 클래스를 정의했습니다.

package com.example.trying;

import android.content.Context;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.widget.TextView;

public class SquareTextView extends AppCompatTextView {
    public SquareTextView(Context context){
        super(context);
    }
    public SquareTextView(Context context, AttributeSet attrs){
        super(context, attrs);
    }
    public SquareTextView(Context context, AttributeSet attrs, int defStyleAttr){
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int size=MeasureSpec.getSize(widthMeasureSpec);
        setMeasuredDimension(size, size);
    }
}

그리고, 메인xml에서 이 클래스로 태그를 생성하였습니다.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.trying.MainActivity" >

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <com.example.trying.SquareTextView
                android:layout_width="0px"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#55ff0000"
                android:text="1"
                android:gravity="center"/>

            <com.example.trying.SquareTextView
                android:layout_width="0px"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#5500ff00"
                android:text="2"
                android:gravity="center"/>

            <com.example.trying.SquareTextView
                android:layout_width="0px"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#550000ff"
                android:text="3"
                android:gravity="center"/>

            <com.example.trying.SquareTextView
                android:layout_width="0px"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#55ff0000"
                android:text="4"
                android:gravity="center"/>

            <com.example.trying.SquareTextView
                android:layout_width="0px"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#5500ff00"
                android:text="5"
                android:gravity="center"/>

            <com.example.trying.SquareTextView
                android:layout_width="0px"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#550000ff"
                android:text="6"
                android:gravity="center"/>

            <com.example.trying.SquareTextView
                android:layout_width="0px"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#55ff0000"
                android:text="7"
                android:gravity="center"/>

            <com.example.trying.SquareTextView
                android:layout_width="0px"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#5500ff00"
                android:text="8"
                android:gravity="center"/>

            <com.example.trying.SquareTextView
                android:layout_width="0px"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#550000ff"
                android:text="9"
                android:gravity="center"/>

        </LinearLayout>
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

정사각형 9개가 화면의 첫번째 줄을 채워줄 것으로 기대했으나,

에러와 함께, 여전히 정사각형 모양은 적용되지 않고 있습니다.

실력이 부족하여, 이 문제를 해결할 수가 없습니다. 

도와주십쇼~

라춘자 (400 포인트) 님이 2017년 12월 17일 질문

1개의 답변

0 추천
 
채택된 답변
익명사용자 님이 2017년 12월 18일 답변
라춘자님이 2018년 4월 12일 채택됨
...