常に[Android]GridLayoutプロジェクトを正方形にする
7737 ワード
私はギャラリーアプリケーションを作っています.グリッドビューのspan countが変わるたびに、ビューの幅は画面の幅に応じて自動的に調整され、矩形になります.
これを正方形に変えてもらえませんか.一日中ネットで検索して、やっと答えを見つけました.
答えはlayoutをcustomに設定し、
長方形のGridViewは、以下のようによく見られます.
これを正方形に変えてもらえませんか.一日中ネットで検索して、やっと答えを見つけました.
答えはlayoutをcustomに設定し、
onMeasure()
メソッドの横と縦の長さに変換するだけでよい.import android.content.Context
import android.util.AttributeSet
import android.widget.FrameLayout
class SquareFrameLayout : FrameLayout {
constructor(context: Context?) : super(context!!) {}
constructor(context: Context?, attrs: AttributeSet?) : super(
context!!, attrs
) {
}
constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(
context!!, attrs, defStyle
) {
}
public override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
// 여기가 핵심!
super.onMeasure(widthMeasureSpec, widthMeasureSpec)
}
}
カスタムレイアウトをxmlファイルに適用できるようになりました.<?xml version="1.0" encoding="utf-8"?>
<com.example.gallery_dg.util.SquareFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/ivPicturePreview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@tools:sample/avatars" />
</com.example.gallery_dg.util.SquareFrameLayout>
長方形のGridViewは、以下のようによく見られます.
Reference
この問題について(常に[Android]GridLayoutプロジェクトを正方形にする), 我々は、より多くの情報をここで見つけました https://velog.io/@blucky8649/Android-GridLayout-아이템을-항상-정사각형으로-만들기テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol