假设我要设置水平方向的线性布局,那么匹配父对象的总像素应该是多少,以便可以根据该大小调整按钮和文本视图。
我试图放置所有边距并进行计算...
预期输出取决于屏幕尺寸。
答案 0 :(得分:0)
match parent
的总像素是屏幕尺寸的总像素,如果要为视图实现自动调整大小,则应使用constraint layout
作为视图的父级布局,使用这个很棒的库,它使用一个称为sdp
https://github.com/intuit/sdp
答案 1 :(得分:0)
没有用户像素,它使您的屏幕无法对所有屏幕尺寸做出响应。如果您希望屏幕对所有屏幕尺寸都响应,请使用ConstraintLayout 或Relative Layout。
以下是使用ConstraintLayout的视图宽度等于所有屏幕尺寸的视图的示例:
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
答案 2 :(得分:0)
match_parent
的总像素取决于布局中视图的parentView
类型,尺寸和组成,例如,如果您的parentView
是LinearLayout
大小100px * 100px
,则您在该match_parent
内的视图的LinearLayout
为100px * 100px
(如果您在LinearLayout
内只有一个视图,否则取决于您的构图)您可以通过对以下任何height
或width
view
和Layout
(以像素为单位)
Java
view.post(new Runnable(){
@Override
public void run() {
int height = view.getMeasuredHeight();
int width = view.getMeasuredWidth();
}
});
kotlin
view.post {
val width = view.measuredWidth
val height = view.measuredHeight
}
获取布局的高度和宽度后,您可以根据逻辑来管理边距或大小