มีวิธีสร้าง NativeScript GridLayout ด้วยอัตราส่วนคงที่หรือไม่

ก่อนอื่น ฉันต้องขออภัยหากไม่ปฏิบัติตามกฎ SO ใดๆ เนื่องจากนี่เป็นคำถามแรกของฉัน

สิ่งที่ฉันพยายามทำให้สำเร็จคือการมี สี่เหลี่ยมจัตุรัส gridLayout ในหน้าของฉัน ลักษณะการทำงานควรเทียบเท่ากับการมีองค์ประกอบที่มี Stretch="aspectFit"

หากวิธีแก้ปัญหาที่แนะนำสามารถให้ฉันบังคับใช้ อัตราส่วนภาพใดๆ ที่กำหนด แทนที่จะใช้แค่อัตราส่วนสี่เหลี่ยมจัตุรัสได้ ก็จะเป็นข้อดีที่ดี เพราะมันจะทำให้ฉันสามารถแก้ไขสถานการณ์หลายๆ สถานการณ์ได้ในอนาคต หรือหากนำไปใช้กับองค์ประกอบเค้าโครงใดๆ ก็ได้ก็จะดีมาก

วิธีแก้ปัญหาอาจแฮ็กได้ตามที่คุณต้องการ และ GridLayout สามารถซ้อนได้ด้วยวิธีใดก็ได้ที่คุณต้องการเพื่อให้บรรลุผลสำเร็จ

ฉันได้ลองแฮ็กมันแล้ว:

 <GridLayout rows="*,auto,*" columns="*,auto,*" backgroundColor="green">
  <Image row="1" col="1" src="~/Images/1px-black.png" stretch="aspectFit" width="100%"></Image>
  <StackLayout row="1" col="1" orientation="vertical" backgroundColor="lightgray">
    <Label text="Label 1" width="100%" height="25%" backgroundColor="red"></Label>
    <Label text="Label 2" width="100%" height="25%" backgroundColor="green"></Label>
    <Label text="Label 3" width="100%" height="25%" backgroundColor="blue"></Label>
    <Label text="Label 4" width="100%" height="25%" backgroundColor="yellow"></Label>
  </StackLayout>
</GridLayout>

ดูเหมือนว่ามันจะใช้งานได้ แต่เนื้อหาของ GridLayout ไม่ได้เติมเต็มอย่างเหมาะสม

PS: ฉันเพิ่งสังเกตเห็นในตัวอย่างที่ฉันเลือก องค์ประกอบเค้าโครงที่ฉันพยายามสร้างเป็นสี่เหลี่ยมจัตุรัสนั้นเป็น StackLayout ที่ซ้อนกัน แต่ก็เป็นแนวคิดเดียวกัน ฉันต้องการวิธีสร้างองค์ประกอบเค้าโครงที่ซ้อนกันเป็นกำลังสอง


person Uka Lpts    schedule 08.07.2016    source แหล่งที่มา
comment
คุณยินดีที่จะมี JS บางตัวที่ทำงานบนเหตุการณ์ที่โหลดหรือไม่? ถ้าเป็นเช่นนั้นนี่เป็นเรื่องเล็กน้อยที่ต้องทำ หากคุณต้องการให้เป็น XML ล้วนๆ จะเป็นไปไม่ได้เนื่องจากไม่มีสิ่งใดจำกัดค่า Y ถึง X (หรือ X ถึง Y) ที่สร้างไว้ในตาราง   -  person Nathanael    schedule 09.07.2016
comment
@Nathanael ฉันเปิดรับวิธีแก้ปัญหาใด ๆ ฉันอยู่ในช่วงเริ่มต้นของการพัฒนา ดังนั้นฉันคิดว่าฉันสามารถรับมือกับวิธีแก้ปัญหาได้แทบทุกอย่าง   -  person Uka Lpts    schedule 09.07.2016
comment
@Nathanael ฉันควรระบุด้วยว่าฉันกำลังทำงานกับแอปพลิเคชัน Angular2 บางทีเหตุการณ์หรือวงจรชีวิตอาจส่งผลต่อคำตอบ   -  person Uka Lpts    schedule 09.07.2016


คำตอบ (3)


ขอบคุณ @Nathanael ที่ให้ข้อมูลเชิงลึกเกี่ยวกับสิ่งที่ฉันสามารถทำได้

ฉันพบวิธีแก้ปัญหาแล้ว และนี่คือวิธีแก้ปัญหาสำหรับทุกคนที่อาจต้องการมัน

ก่อนอื่น ฉันจะใช้เทมเพลตต่อไปนี้:

<GridLayout rows="2*,3*,*,3*,*,3*,*,3*,2*" columns="2*,3*,*,3*,*,3*,*,3*,2*" backgroundColor="green" #refGrid>
    <Label text="A1" col="1" row="1" backgroundColor="#899bfe"></Label>
    <Label text="A2" col="1" row="3" backgroundColor="#899bfe"></Label>
    <Label text="A3" col="1" row="5" backgroundColor="#899bfe"></Label>
    <Label text="A4" col="1" row="7" backgroundColor="#899bfe"></Label>

    <Label text="B1" col="3" row="1" backgroundColor="#899bfe"></Label>
    <Label text="B2" col="3" row="3" backgroundColor="#899bfe"></Label>
    <Label text="B3" col="3" row="5" backgroundColor="#899bfe"></Label>
    <Label text="B4" col="3" row="7" backgroundColor="#899bfe"></Label>

    <Label text="C1" col="5" row="1" backgroundColor="#899bfe"></Label>
    <Label text="C2" col="5" row="3" backgroundColor="#899bfe"></Label>
    <Label text="C3" col="5" row="5" backgroundColor="#899bfe"></Label>
    <Label text="C4" col="5" row="7" backgroundColor="#899bfe"></Label>

    <Label text="D1" col="7" row="1" backgroundColor="#899bfe"></Label>
    <Label text="D2" col="7" row="3" backgroundColor="#899bfe"></Label>
    <Label text="D3" col="7" row="5" backgroundColor="#899bfe"></Label>
    <Label text="D4" col="7" row="7" backgroundColor="#899bfe"></Label>
</GridLayout>

เป้าหมายของฉันคือการทำให้ GridLayout ดูเป็นสี่เหลี่ยมจัตุรัสและอยู่ตรงกลางบนหน้าจอ ไม่ว่าอุปกรณ์หรืออัตราส่วนภาพของหน้าจอจะเป็นเช่นใดก็ตาม เพื่อให้บรรลุผลนี้ ฉันได้ทำสิ่งต่อไปนี้ใน JavaScript (หรือ TypeScript ในกรณีนี้ เนื่องจากฉันกำลังทำงานกับ Angular2 + Nativescript) ภายใน Component Definition

ngAfterViewInit() {
    let grid: GridLayout = <GridLayout>this.refGrid.nativeElement;
    var x = grid.getActualSize();

    function wait() {
        if (x.width <= 0) {
            x = grid.getActualSize();
            setTimeout(wait, 10);
        } else {
            grid.width=Math.min(x.width, x.height);
            grid.height=Math.min(x.width, x.height);          
        }
    }
    wait();
}

และนี่คือผลลัพธ์:

Square GridLayout ในความละเอียดหน้าจอและอัตราส่วนภาพที่หลากหลาย

person Uka Lpts    schedule 10.07.2016

คุณสามารถใช้คุณสมบัติดั้งเดิมของโครงร่างกริดได้ คุณจะต้องตรวจสอบให้แน่ใจว่าตารางของคุณเต็มทั้งหน้าจอ (สามารถทำได้โดยการกำหนดเป้าหมายด้วยคลาส css) จากนั้น คุณสามารถสร้างความละเอียดที่คุณต้องการในแถวและคอลัมน์ สังเกตการใช้ x* เพื่อกำหนดความกว้างสัมพัทธ์ การสาธิตที่ฉันเลือกมี 10 ดังนั้น * แทน 10%

จากนั้นคุณสามารถซ้อนเค้าโครงอื่นๆ ในเซลล์กริดได้หากต้องการ

<GridLayout rows="*,*,*,*" columns="*,2*,3*,4*">
  <Label row="0" col="0" [colSpan]="4" backgroundColor="lightgray"> // Will be 100% width, height will be 10%
  </Label>
  <Label row="1" col="0" [colSpan]="4" backgroundColor="blue"> // Will be 100% width, height will be 20%
  </Label>
  <Label row="2" col="0" [colSpan]="4" backgroundColor="green"> // Will be 100% width, height will be 30%
  </Label>
  <Label row="3" col="0" [colSpan]="4" backgroundColor="red"> // Will be 100% width, height will be 40%
  </Label>
</GridLayout>

หวังว่าจะช่วยได้

person George Edwards    schedule 10.07.2016

รหัสด้านล่างใช้งานได้สำหรับฉัน - ใช้พื้นที่ทั้งหมดของหน้าจอ คุณอาจต้องการสิ่งที่แตกต่างสำหรับหน้าจอที่ใหญ่กว่า

<FlexboxLayout flexDirection="row" flexWrap="wrap">
    <GridLayout flexGrow="1" row="*" columns="*" *ngFor="let tile of tiles" [width]="len" [height]="len">
        <StackLayout horizontalAlignment="center" verticalAlignment="center">
            <Label [text]="tile.line"></Label>
        </StackLayout>
    </GridLayout>
</FlexboxLayout>

ค่า len ถูกคำนวณดังนี้:

import {isAndroid, isIOS, screen} from "tns-core-modules/platform";
...
ngAfterViewInit() { 
    this.len = screen.mainScreen.widthDIPs / 2;
}
person Leukipp    schedule 06.01.2020