2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

androidで四角いレイアウト

Posted at

androidでkotlinでの四角いレイアウトの実装を示す。

このスタックオーバーフローなどは情報が古くなっている。


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:showIn="@layout/activity_main"
        tools:context=".MainActivity"
        android:background="#000000">


        <FrameLayout
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginEnd="0dp"
                android:layout_marginStart="0dp"
                android:layout_marginTop="0dp"
                app:layout_constraintDimensionRatio="H,1:1"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                android:background="@android:color/background_light">
        </FrameLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

app:layout_constraintDimensionRatio="H,1:1"のHをWにすれば制約内での最大の幅に合うようになる。また1:2なども可能である。

package com.example.myapplication

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity;

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

#出力

geagaeg.png

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?