Android中如何使控制項保持固定寬高比

2021-09-29 15:43:35 字數 1532 閱讀 9475

如何實現固定的寬高比,現在提供兩種方案。

自定義ratioview,重寫onmeasure或onlayout等相關方法,通過預定的比例計算寬高。

**:

public

class

ratioview

extends

view

public

ratioview

(context context,

@nullable attributeset attrs)

public

ratioview

(context context,

@nullable attributeset attrs,

int defstyleattr)

@override

protected

void

onmeasure

(int widthmeasurespec,

int heightmeasurespec)

super

.onmeasure

(widthmeasurespec, heightmeasurespec);}

}

布局**

android:id

="@+id/ratio_rv"

android:layout_width

="0dp"

android:layout_height

="wrap_content"

android:background

="#30ff0000"

="parent"

="parent"

="parent"

/>

2. constraintlayout的dimensionratio

**如下:

android:layout_width

="match_parent"

android:layout_height

="wrap_content"

="@+id/ratio_rv"

>

android:layout_width

="0dp"

android:layout_height

="0dp"

android:background

="#300000ff"

="2:1"

="parent"

="parent"

="parent"

/>

androidx.constraintlayout.widget.constraintlayout

>

constraintdimensionratio="h,1:2"中的 w or h 指後面比例的第1個是那個方向的,預設是w(即寬度)第乙個。

上圖

Android之EditText控制項的使用

這裡我們先總結一下android控制項的使用規律 1 給控制項定義乙個id 2 指定控制項的高度和寬度 3 加入某些控制項特有的屬性即可 現在開始edittext控制項的使用 在布局檔案中加入 這裡的hint的作用是會在你的輸入框中顯示一些提示性的文字,一旦你輸入了內容提示性的文字就會消失,這樣無疑...

Android 如何保持螢幕常亮

for example public class mainactivity extends activity 這個方法的好處是,與wakelocks不同 我們將在keep the cpu on文章中 它不需要具體的許可權,並且在換不同應用程式的操作中,系統會管理,不必擔心沒有釋放的未使用的資源。另一...

qt 中控制項保持比例縮放

最終效果 實現思路是這樣的 封裝乙個類,整合自qframe 繼承自qframe而不是qwidget是因為qframe過載了paintevent,方便進行樣式美化 在這個類中宣告乙個qwidget的成員變數,重寫派生類的這個方法 virtual void resizeevent qresizeeven...