【问题标题】:Divide screen by percentage from top and sides从顶部和侧面按百分比划分屏幕
【发布时间】:2014-02-04 20:57:26
【问题描述】:

我正在尝试创建一个布局,其中图像位于左侧 20% 的屏幕尺寸、右侧 20% 的屏幕尺寸和顶部 30% 的屏幕尺寸。我可以将图像定位为距侧面距离的 20%,但不知道如何同时结合顶部和侧面的百分比。现在我使用 android:layout_marginTop 从顶部边缘跳转。我想使用一些可以在任何屏幕分辨率上从顶部腾出 30% 可用屏幕空间的东西。请看图1。非常感谢您的帮助。

XML 代码:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="100" >

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="20" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:layout_marginTop="100dp"
        android:layout_weight="60"
        android:src="@drawable/logo_line" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="20" />
</LinearLayout>

【问题讨论】:

    标签: android android-layout screen-resolution


    【解决方案1】:

    使用 RelativeLayout 或将 ImageView 包装在 LinearLayout 中:

    ---- 编辑----

    在 ImageView 上:取出 layout_gravity 和 margin_top 属性并将 layout_height 设置为 0dp

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="100" >
    
    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="20" />
    
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="60"
        android:orientation="vertical"
        android:weightSum="100" >
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="30" />
    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="match_parent"
        android:layout_height="0dp"
         android:layout_weight="40"
        android:src="@drawable/logo_line" />
    </LinearLayout>
    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="20" />
    </LinearLayout>
    

    【讨论】:

    • 我对此进行了少许编辑,但终于成功了!太棒了,非常感谢!
    【解决方案2】:

    对此我有一个动态解决方案:

    final Display display = getWindowManager().getDefaultDisplay();
    final Point size = new Point();
    display.getSize(size);
    
    final WindowManager.LayoutParams params = getWindow().getAttributes();
    params.width = (int) (size.x * 0.6);
    params.height = (int) (size.y * 0.6);
    

    这将是高度(100% - (20% + 20%))和宽度相同的 20% 折扣。

    ---- 编辑----

    上述解决方案是格式化布局窗口本身。要做到这一点,只需一个布局项,我会这样做:

      final TextView tv = findViewById(R.id.my_text_view);
    
      LinearLayout.LayoutParams tvlp = (LinearLayout.LayoutParams) tv.getLayoutParams();
    
      tvlp.rightMargin *= 0.6;
      tvlp.leftMargin *= 0.6;
      tvlp.topMargin *= 0.6;
      tvlp.bottomMargin *= 0.6;
    

    这将重新调整TextView 的布局。

    【讨论】:

    • 谢谢。这一段代码看起来非常好。您能否告诉我如何仅将它用于布局中的一个对象。因为这段代码对布局中的所有对象都有影响。
    • 查看我的编辑,我添加了一个重新调整TextView 的示例。希望对您有所帮助。
    • 不知道是不是我做错了,但是没有效果。所有的 TextView 都在屏幕的整个宽度上。
    • 要小心,因为它还取决于包含它们的Layout 的布局参数!如果宽度和高度都是match_parent,您将看不到任何更改。然后设置为wrap_content,以便您可以看到更改。
    • 我找不到问题所在。这是代码:clip2net.com/s/6Kzi5c 这是 XML:clip2net.com/s/6Kzkn4 你能看一下吗?
    【解决方案3】:

    将 ImageView 放在另一个 LinearLayout 中,这样您就可以设置水平权重(ImageView 所在的 LinearLayout)和另一个用于垂直权重

    已编辑

        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="30" />
        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="horizontal"
            android:weigth="40"
            android:weightSum="100" >
    
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="20" />
    
        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="60"
            android:src="@drawable/logo_line" />
    
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="20" />
        </LinearLayout>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="30" />
        </LinearLayout>
    

    这应该可以工作。重要的是视图的正确配置和线性布局的方向。 创建一个LinearLayout,其中包含 3 个“项目”,其宽度与其父项匹配。 您可以将它们全部设置为创建垂直(顶部/底部)边距的权重。 然后 LinearLayout 中的第二项应该是另一个创建水平(左/右)边距的 LinearLayout。这个 LinearLayout 应该是水平的。

    这不是一个干净的边距解决方案。如果它不起作用,你应该动态地做它

    让我知道它是否有效

    【讨论】:

    • 我之前也尝试过这个解决方案,但它仅对侧面的 20% 有效。从上到下 30% 没有效果。想知道哪里有问题?
    • 我知道我可能在哪里犯了一个小错误..我现在没有时间修复它..我尽快修复它所以在几个小时内再次检查我的答案..它可能那么应该可以工作...顺便说一句,您可以在尝试相同方法的地方发布脚本吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-09
    • 2018-06-18
    • 2021-01-21
    • 2014-10-17
    相关资源
    最近更新 更多