【问题标题】:Layout: center image inside center panel. Using percent units布局:中心面板内的中心图像。使用百分比单位
【发布时间】:2020-08-09 12:51:16
【问题描述】:

我正在尝试在我的一个活动布局中获得类似于以下内容的设计:

即:

  • 一个白色面板,宽度是其父面板的 60%。水平居中。
  • 在该白色面板内,面板宽度为 60% 的图像,居中 水平方向。

我一直在使用 Guidelines 将它们水平设置为 0.2、0.4、0.6 和 0.8,但没有成功。 我怎样才能得到它?

【问题讨论】:

    标签: android user-interface layout design-guidelines


    【解决方案1】:

    您必须使用 ConstraintLayout 作为根布局,然后将孩子的宽度设置为 0dp 并将宽度百分比设置为 0.6(对于第一个容器,此设置为 60%):

            <FrameLayout
                android:id="@+id/some_id"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent
                app:layout_constraintWidth_percent=".6" >
    

    对于ImageView来说,本身就是60%的viewgroup的60%,所以需要是parent的36%:

            <ImageView
                android:id="@+id/some_other_id"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent
                app:layout_constraintWidth_percent=".36" />
    

    另一种选择是将 ImageView 的父级设为 ConstraintLayout,然后将 ImageView 的宽度设置为 60%,但这样您的布局将不会平坦。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-05
      • 1970-01-01
      • 1970-01-01
      • 2019-09-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多