【问题标题】:How to reduce size of LinearLayout with Background Image large one如何使用大背景图像减小 LinearLayout 的大小
【发布时间】:2015-09-30 14:30:41
【问题描述】:

我正在尝试垂直绘制 2 个布局,顶部一个包含图像,由于图像非常大,它占据了下半部分的一部分,我使用 weights =1 和 layoutheight=0dp 对其进行平均划分,

没有任何图像,分区是完美的,但是如果我将背景图像设置为播放,重量比不完美,不能重复使用,

我试过用framelayout,也没用,

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

【问题讨论】:

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


    【解决方案1】:

    试试这个:

    <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>
    

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

    【讨论】:

    • 添加到您的主布局android:weightSum="1"
    • @ArturSzymański 感谢您的评论。
    • 这个不行,当你为layout设置的图片集较大时,不影响权重。
    • @Rakesh 您在哪个布局上应用背景图片?
    • 嗨伙计们,终于明白了问题的根源,这不是由于图像,而是由于我添加到线性布局后半部分的coverflow,但它在运行时运行良好。跨度>
    【解决方案2】:

    如果你想给你的视图相对空间,这里有一个新的 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>
    

    【讨论】:

      猜你喜欢
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 2012-03-17
      • 1970-01-01
      • 2015-06-04
      • 2014-06-05
      相关资源
      最近更新 更多