簡體   English   中英

如何在移動屏幕上水平和垂直居中Framelayout

[英]How to center Framelayout horizontally and vertically in Mobile screen

我在Linearlayout中使用Framelayout.Frmelayout包含兩個imageview。 我希望將整個framelayout居中。 我可以將Framlyout內的子項與屬性gravity:center對齊。 但是問題是,當我使用它時,兩個圖像視圖都居中對齊,但是我希望第二個圖像出現在第一個圖像的邊框(即相機圖標)上,以便用戶單擊圖像即可知道他們可以編輯圖片。

這是我的框架布局

 <Linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <FrameLayout android:id="@+id/userimage" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp"> <com.pkmmte.view.CircularImageView android:id="@+id/regUserPhoto" android:layout_width="100dp" android:layout_height="100dp" android:src="@drawable/drawable_male" android:elevation="4dp" android:layout_marginLeft="100dp" app:border="true" android:layout_marginBottom="8dp" app:border_color="#EEEEEE" app:border_width="4dp" app:shadow="true" /> <ImageView android:id="@+id/iv_camera" android:layout_marginLeft="100dp" android:layout_width="40dp" android:layout_height="40dp" android:src="@drawable/camera_button" /> </FrameLayout> </Linearlayout> 

提前致謝。

你可以做到這一點使用超快速CostraintLayout ,你可以做到這一點Linearlayout ,但它會導致你的布局變得非常嵌套布局(可能影響更復雜layous你的表現)。

這是您想要的布局的示例:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layoutDirection="ltr"
  android:orientation="vertical">

<Button
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    app:layout_constraintEnd_toEndOf="@+id/play_button"
    app:layout_constraintHeight_percent=".1"
    app:layout_constraintTop_toTopOf="@+id/play_button"
    android:text="PIC"/>

<ImageView
    android:id="@+id/play_button"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHeight_percent=".5"
    app:layout_constraintWidth_percent=".5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:scaleType="fitXY"
    app:srcCompat="@drawable/rose" />


</android.support.constraint.ConstraintLayout>

您的布局將如下所示:

在此處輸入圖片說明

如果要更改圖像大小,只需更改這些屬性值即可:

app:layout_constraintHeight_percent
app:layout_constraintWidth_percent

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM