Android以高度为百分比排列布局

问题描述 投票:14回答:5

我对Android和练习都很陌生。我正在尝试设计一个屏幕。它将包含一个背景图像和一个带滑动菜单的浮动容器。 (有关详细信息,请参阅附图)

我的布局包括一个背景图片,一个带有一些图标的容器,这些图标浮在底部,但底部有一些边缘(见附图)

据我所知,这可以通过在底部安排“相对布局”并在其中放置图像来实现。这是对的吗 ?

另外,我想添加一个重复的透明图像作为浮动div的背景。

请给我一个很好的建议或指点我一个很好的教程

提前致谢

android android-layout android-relativelayout
5个回答
33
投票

您可以使用LinearLayout并将layout_weight设置为xml中的%

至于重复背景,你可以使用tileMode

示例:请注意,weightSum设置为100,表示总重量为100。有layout_weight=10将给它10%空间分配。

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:layout_gravity="bottom"
    android:weightSum="100">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="10"
        android:background="@drawable/bg"
        android:orientation="horizontal"
        android:tileMode="repeat" >
    </LinearLayout>
    <View 
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="5" />
</LinearLayout>

4
投票

您可以使用新的百分比支持库来实现此目的:https://developer.android.com/tools/support-library/features.html#percent

做这样的事情:

<android.support.percent.PercentRelativeLayout
    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">

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_alignParentBottom="true"
        app:layout_heightPercent="11%"
        app:layout_widthPercent="100%" />

</android.support.percent.PercentRelativeLayout>

1
投票

如果要以百分比划分高度,则需要水平方向的线性布局,并为每个项目添加layout_weight。

linear layout guide


0
投票

根据您的要求,任何布局都可以。您也可以通过线性布局实现此目的。如果我理解您的要求,请查看此讨论。

How to make android app's background image repeat


0
投票
<view android:layout_width="wrap_content"
      class="net.zel.percentage.PercentageButton"
      android:id="@+id/button"
      android:layout_height="wrap_content"

      customView:percentage_width="50"
      customView:percentage_height="50"

      />

所以你可以在库中添加所需的属性 https://github.com/metrolog3005/percentage_view

© www.soinside.com 2019 - 2024. All rights reserved.