如何使用背景图像大缩小LinearLayout的大小

时间:2015-09-30 14:30:41

标签: android android-layout android-linearlayout android-layout-weight

我正在尝试垂直绘制2个布局,其中顶部包含图像,因为Image非常大,占据了一半的部分,我使用权重= 1和layoutheight = 0dp来平均分区,

没有任何图像,分区是完美的,但如果我将背景图像设置为toplayout,重量比不完美,这是不可重用的,

我尝试使用framelayout,这也没用,

在xml中执行此操作的确切方法是什么,而不是以编程方式分配布局高度。

2 个答案:

答案 0 :(得分:0)

试试这个:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5">
   </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5">
    </LinearLayout>
</LinearLayout>

然后,您可以将图像设置为顶部布局的背景。

答案 1 :(得分:0)

如果你想给你的观点相对空间,这里有你可能想尝试的新的android库。

百分比支持库

https://developer.android.com/tools/support-library/features.html#percent

您可以在项目中使用它,如:

<android.support.percent.PercentFrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_heightPercent="50%"
            app:layout_widthPercent="100%" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_gravity="bottom"
            app:layout_heightPercent="50%"
            app:layout_widthPercent="100%" />

    </android.support.percent.PercentFrameLayout>
相关问题