【问题标题】:Linear Layout won't take the height of its parent in percentRelativeLayout线性布局不会在 percentRelativeLayout 中采用其父级的高度
【发布时间】:2017-07-17 19:01:09
【问题描述】:

我正在使用 Xamarin Android,并且我有一个列表视图,其中包含以下内容的行视图:

所有这些视图都包含在PercentRelativeLayout 中,因为我想保持宽度成比例。所以我遇到的问题是,由于左侧的两个布局在运行时留空并填充(绝对没有办法解决这个问题),它们的高度被测量为 0,然后将 Rowview 高度设置为0,然后将控件布局设置为右侧的高度为 0,然后拉伸到其中最大控件的大小(例如:)

我试过了:

  • 使用布局权重和高度 0 允许布局本身拉伸以填充父级

  • 在布局内的控件本身上使用布局权重

  • 完全放弃布局,简单地将图像按钮包装在 PercentRelativeLayout 中,并使用布局位置正确定位它们

  • 在子级中使用基于百分比的布局宽度和高度来填充高度(这似乎只适用于宽度,而不适用于高度)

  • 添加虚拟视图作为填充以将控件布局推入到位

  • 将基于百分比的边距添加到布局本身、子级、填充虚拟视图以及这些的每个组合

我尝试过的任何一件事都没有任何效果,唯一影响线性布局高度的就是对高度进行硬编码。

然后我的问题是,一旦测量完所有内容,我如何才能告诉此布局占据行视图的整个高度? (如果您有任何其他问题可以帮助解决这个问题,请告诉我,我很乐意提供帮助)

编辑 #1:这是我的行视图 xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:orientation="vertical"
  android:layout_width="400dp"
  android:layout_height="match_parent"
  android:id="@+id/LeadRowHolder">

  <!--Tag Holder-->
  <RelativeLayout
    android:layout_height="wrap_content"
    app:layout_widthPercent="80%"
    android:paddingTop="10dp"
    android:orientation="horizontal"
    android:id="@+id/LeadTagLayout">

  </RelativeLayout>

  <!--Details Holder-->
  <LinearLayout
    android:layout_height="match_parent"
    app:layout_widthPercent="80%"
    android:orientation="horizontal"
    android:id="@+id/LeadDetailsLayout"
    android:layout_below="@id/LeadTagLayout"
    android:paddingTop="20dp"
    android:paddingBottom="20dp"
    android:paddingRight="10dp"
    android:paddingLeft="10dp">
    <android.webkit.WebView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/leadShortDescription"/>
  </LinearLayout>
  <!--Controls Holder-->
  <LinearLayout
    android:layout_height="wrap_content"
    app:layout_widthPercent="20%"
    android:orientation="horizontal"
    android:id="@+id/LeadControlsLayout"
    android:layout_toRightOf="@id/LeadTagLayout"
    android:gravity="center_vertical|center_horizontal">
    <ImageButton
      android:layout_height="50dp"
      android:layout_width="50dp"
      android:id="@+id/MenuCall3x"
      android:background="@drawable/MenuCall3x"/>
    <ImageButton
      android:layout_height="30dp"
      android:layout_width="20dp"
      android:id="@+id/MenuFwd3x"
      android:background="@drawable/MenuFwd3x"/>
  </LinearLayout>
</android.support.percent.PercentRelativeLayout>

【问题讨论】:

  • 我认为你的百分比布局应该有wrap_contentlayout_height。为什么是 match_parent?
  • @MatPag 无论哪种方式都不会影响它,但是我选择 match parent 的原因是因为我认为这会给 rowview 一个可以在运行时使用的设定高度,但是高度是计算出来的为零,直到适配器填写列表视图,无论我做什么(因为它不会影响布局,我相信 match_parent 更有效,因为它减少了它采取的措施数量让 Android 计算视图的尺寸,因为它只接受父母计算的高度,除非发生某种我不知道的优化。)
  • 为了澄清,我确实继续尝试你所说的,我的意思是说即使尝试了它也不能解决问题。
  • 我真心不喜欢你的做法。我从来没有在列表的每一行中看到过 webview...如果确实有必要,您应该为 webview 设置一个固定的高度,并且您应该使用不同类型的行布局而不是在运行时手动添加视图相对布局。复杂行的膨胀过程会非常缓慢,并且某些设备上的用户体验可能会受到影响
  • @MatPag WebView 在每一行中都会很慢,除非我们只是加载一个原始 html 字符串,所以它实际上只是在 html 中读取、格式化和显示它,所以它比实际加载网页。需要明确的是,我同意你的看法,它会变慢,这就是公司决定这样做的方式,以简化 Android 和 iOS 上的文本格式。

标签: android android-layout xamarin xamarin.android


【解决方案1】:

我用你的代码玩了一下,结果看起来不错。

<android.support.percent.PercentRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:id="@+id/LeadRowHolder">

    <!--Tag Holder-->
    <RelativeLayout
        android:layout_height="wrap_content"
        app:layout_widthPercent="80%"
        android:orientation="horizontal"
        android:id="@+id/LeadTagLayout">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="this is a test"/>

    </RelativeLayout>

    <!--Details Holder-->
    <LinearLayout
        android:layout_height="wrap_content"
        app:layout_widthPercent="80%"
        android:orientation="horizontal"
        android:id="@+id/LeadDetailsLayout"
        android:layout_marginTop="5dp"
        android:layout_below="@id/LeadTagLayout">
        <android.webkit.WebView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="50dp"
            android:id="@+id/leadShortDescription"/>
    </LinearLayout>
    <!--Controls Holder-->
    <LinearLayout
        android:layout_height="wrap_content"
        app:layout_widthPercent="20%"
        android:layout_centerVertical="true"
        android:orientation="horizontal"
        android:id="@+id/LeadControlsLayout"
        android:layout_toRightOf="@id/LeadTagLayout"
        android:gravity="center_vertical|center_horizontal">
        <ImageButton
            android:layout_height="50dp"
            android:layout_width="50dp"
            android:id="@+id/MenuCall3x"/>
        <ImageButton
            android:layout_height="30dp"
            android:layout_width="20dp"
            android:id="@+id/MenuFwd3x"/>
    </LinearLayout>
</android.support.percent.PercentRelativeLayout>

让我知道这在运行时是否适合您(我已经删除了一些您没有提供来测试此 XML 的背景可绘制文件)。 我在 RelativeLayout 中添加了一个 TextView 来测试他的身高

【讨论】:

  • 虚幻,我所需要的只是 centerVertical="True"....非常感谢,我已经坚持了好几个小时了。
  • 我确实继续为 webviews 添加了 minHeight,这有什么好处?只是出于好奇。
  • 它应该有助于计算视图的高度。但这取决于您在其中加载的内容。您可以删除它,它应该可以正常工作。
  • 不要忘记 Percent 库从支持库 26.X.X 开始已被弃用。它已被ConstraintLayout library 取代
  • 我注意到那天晚上,我会牢记这一点。
猜你喜欢
  • 1970-01-01
  • 2014-02-21
  • 2021-05-03
  • 1970-01-01
  • 2020-05-07
  • 1970-01-01
  • 2019-08-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多