Android 水平和垂直布局的重量

Android 水平和垂直布局的重量,android,android-layout,android-linearlayout,Android,Android Layout,Android Linearlayout,我的情况是这样的: <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <RelativeLayout android:layout_width="0dp" android:layout_height="wrap_conte

我的情况是这样的:

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

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.6" >

        <!-- using relativelayout I can match background size to content -->

        <View
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/riderTable"
            android:layout_alignLeft="@id/riderTable"
            android:layout_alignRight="@id/riderTable"
            android:layout_alignTop="@+id/riderTable"
            android:background="@drawable/rider_opisowe" />

        <TableLayout
            android:id="@+id/riderTable"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="2dp"
            android:shrinkColumns="*" >

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <!-- TextViews here -->
            </TableRow>
            <!-- more rows -->

        </TableLayout>
    </RelativeLayout>

    <ImageView
        android:id="@+id/imgPhoto"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:layout_weight="0.4"
        android:adjustViewBounds="true"
        android:src="@drawable/myImage" />

</LinearLayout>

我有这样的布局:

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

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.6" >

        <!-- using relativelayout I can match background size to content -->

        <View
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/riderTable"
            android:layout_alignLeft="@id/riderTable"
            android:layout_alignRight="@id/riderTable"
            android:layout_alignTop="@+id/riderTable"
            android:background="@drawable/rider_opisowe" />

        <TableLayout
            android:id="@+id/riderTable"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="2dp"
            android:shrinkColumns="*" >

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <!-- TextViews here -->
            </TableRow>
            <!-- more rows -->

        </TableLayout>
    </RelativeLayout>

    <ImageView
        android:id="@+id/imgPhoto"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:layout_weight="0.4"
        android:adjustViewBounds="true"
        android:src="@drawable/myImage" />

</LinearLayout>

60%/40%的比率正在发挥作用,但imageview(白色正方形)与蓝色框的高度不匹配。我希望图像容器的宽度为父容器的40%,与蓝色框的高度相同,并且包含图像

已经尝试过这个: 不管这个简单的例子是如何描述的,只要使用一种颜色的背景,如果我切换到图像,这并没有达到预期效果。因此,将其放在另一个线性布局中并不能解决问题


任何帮助都将不胜感激。

将您的
图像视图的
android:layout\u height
wrap\u content
更改为
match\u parent

      <ImageView
            android:id="@+id/imgPhoto"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_gravity="top"
            android:layout_weight="0.4"
            android:adjustViewBounds="true"
            android:src="@drawable/myImage" />

试试这个,可能会有帮助

<ImageView
        android:id="@+id/imgPhoto"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_gravity="top"
        android:layout_weight="0.4"
        android:adjustViewBounds="true"
        android:src="@drawable/myImage" />

您的布局应该更像:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="horizontal" 
    >

    <FrameLayout
        android:id="@+id/left"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:layout_weight="6" />

    <FrameLayout
        android:id="@+id/right"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:layout_weight="4" />

</LinearLayout>


您可以随意替换左右框架布局。

如果可能,请添加图像。谢谢,我不认为我离正确的解决方案太近了!几个小时的尝试,就是这样!有时它会发生。