Как да направите перфектно квадратно изображение/бутон

Проектирам основна ИГРА Sudoko за android. Искам таблица 4x4 с всички клетки като квадрати в нея.

Опитвам това с 16-те бутона в TableLayout.

Моят начин изглежда така

  • въведете описание на изображението тук

имат правоъгълна форма :(

моя xml

<TableLayout
android:id="@+id/tl"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:gravity="center" >

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:gravity="center" >

    <Button
        android:id="@+id/button1"
        style="@style/box_sky_blue"
        android:layout_weight="1"
        android:text="1" />

    <Button
        android:id="@+id/button2"
        style="@style/box_sky_blue"
        android:layout_weight="1"
        android:text="2" />

    <Button
        android:id="@+id/button3"
        style="@style/box_sky_blue"
        android:layout_weight="1"
        android:text="3" />

    <Button
        android:id="@+id/button4"
        style="@style/box_sky_blue"
        android:layout_weight="1"
        android:text="4" />
</TableRow>

<TableRow
    android:id="@+id/tableRow2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center" >

    <Button
        android:id="@+id/button5"
        style="@style/box_sky_blue"
        android:layout_weight="1"
        android:text="5" />

    <Button
        android:id="@+id/button6"
        style="@style/box_sky_blue"
        android:layout_weight="1"
        android:text="6" />

    <Button
        android:id="@+id/button7"
        style="@style/box_sky_blue"
        android:layout_weight="1"
        android:text="7" />

    <Button
        android:id="@+id/button8"
        style="@style/box_sky_blue"
        android:layout_weight="1"
        android:text="8" />
</TableRow>

<TableRow
    android:id="@+id/tableRow3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center" >

    <Button
        android:id="@+id/button9"
        style="@style/box_sky_blue"
        android:layout_weight="1"
        android:text="9" />

    <Button
        android:id="@+id/button10"
        style="@style/box_sky_blue"
        android:layout_weight="1"
        android:text="7" />

    <Button
        android:id="@+id/button11"
        style="@style/box_sky_blue"
        android:layout_weight="1"
        android:text="8" />

    <Button
        android:id="@+id/button12"
        style="@style/box_sky_blue"
        android:layout_weight="1"
        android:text="7" />
</TableRow>

<TableRow
    android:id="@+id/tableRow3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center" >

    <Button
        android:id="@+id/button13"
        style="@style/box_sky_blue"
        android:layout_weight="1"
        android:text="9" />

    <Button
        android:id="@+id/button14"
        style="@style/box_sky_blue"
        android:layout_weight="1"
        android:text="7" />

    <Button
        android:id="@+id/button15"
        style="@style/box_sky_blue"
        android:layout_weight="1"
        android:text="8" />

    <Button
        android:id="@+id/button16"
        style="@style/box_sky_blue"
        android:layout_weight="1"
        android:text="7" />
</TableRow>
</TableLayout>

ето кутия небесно синьо

<style name="box_sky_blue">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:background">@color/box_color_sky_blue</item>
    <item name="android:layout_margin">5dp</item>
    <item name="android:padding">5dp</item>
    <item name="android:textSize">20sp</item>
    <item name="android:gravity">center</item>
    <item name="android:textColor">#ffffff</item>
</style>

Как да ги направя квадратни, тъй като имам квадрат от 4x4,5x5 & 6x6


person Maveňツ    schedule 24.07.2014    source източник
comment
можете ли да публикувате код на box_sky_blue.xml от style   -  person Android is everything for me    schedule 24.07.2014
comment
@Androidiseverythingforme моля прегледайте :)   -  person Maveňツ    schedule 24.07.2014
comment
ако използвате ограничено оформление и височината е зададена на 0 dp, тогава задайте ширината на 0dp и използвайте това приложение:layout_constraintDimensionRatio=H,1:1   -  person Abdulrahman Abdelkader    schedule 23.07.2020
comment
@AbdulrahmanAbdelkader Преди 4 години оформлението на ограниченията не беше въведено в sdk, дойде през 2018 г.   -  person Maveňツ    schedule 24.07.2020


Отговори (3)


Променете wrap_contents на размер по подразбиране:

android:layout_width="wrap_content"
android:layout_height="wrap_content"

to

android:layout_width="@dimen/box_size"
android:layout_height="@dimen/box_size"

(и след това задайте box_size в res/values/dimen.xml като: <dimen name="box_size">50dp</dimen>)


ИЛИ използвайте wrap_content за ширината и след това в кода използвайте myBox.setHeight(myBox.getMeasuredWidth);, така че ширината и височината да съвпадат. Просто се уверете, че изгледът е напълно зареден, в противен случай getMeasuredWidth връща 0.

РЕДАКТИРАНЕ:

За да промените височината, за да съответства на ширината на wrap_content, след като изгледът е зареден, можете да използвате ViewTreeObserver:

if(yourActivityLayout.getViewTreeObserver().isAlive()){
    yourActivityLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout(){
            // The view is completely loaded now, so getMeasuredWidth() won't return 0
            yourButton1.setLayoutParams(new TableLayout.LayoutParams(yourButton1.getMeasuredWidth(), yourButton1.getMeasuredWidth()));
            ... // Do this for all buttons (preferably in a for-loop to prevent repetition)

            // Destroy the onGlobalLayout afterwards, otherwise it keeps changing
            // the sizes non-stop, even though it's already done
            yourActivityLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    });
}
person Kevin Cruijssen    schedule 24.07.2014

Код на бутон в XML

<Button
    android:id="@+id/btn"  
    android:layout_width="64dp"
    android:layout_height="64dp"
    android:background="@drawable/buttonshape"/>

код на buttonshape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
 android:shape="rectangle" >
<corners
android:topLeftRadius="0dp"
android:topRightRadius="0dp"
android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp"/>
<solid
android:color="#0000ff"/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"/>
<size
android:width="64dp"
android:height="64dp"/>
</shape>
person Android is everything for me    schedule 24.07.2014
comment
това е твърдо кодирано. Трябва да предоставите решение, което не е твърдо кодирано. - person Yash Jain; 01.09.2020

Опитайте се да добавите фиксирана ширина и височина.

 android:layout_width="@dimen/box_size"
 android:layout_height="@dimen/box_size"

и в res/values/dimen.xml add

 <dimen name="box_size">40dp</dimen>
person nayana bhoj    schedule 23.01.2017