Android:使ImageView与另一个视图的高度匹配
我在设计应用程序的布局时遇到问题。这就是我想要得到的:Android:使ImageView与另一个视图的高度匹配,android,layout,imageview,height,android-linearlayout,Android,Layout,Imageview,Height,Android Linearlayout,我在设计应用程序的布局时遇到问题。这就是我想要得到的: ========================================================= | ============= ================ ================ | | | | | LinearLayout | | LinearLayout | | | | | | ============ | | ========
=========================================================
| ============= ================ ================ |
| | | | LinearLayout | | LinearLayout | |
| | | | ============ | | ============ | |
| | | | | TextView | | | | TextView | | |
| | ImageView | | ============ | | ============ | |
| | | | ============ | | ============ | |
| | | | | TextView | | | | TextView | | |
| | | | ============ | | ============ | |
| | | | ============ | | ============ | |
| | | | | TextView | | | | TextView | | |
| | | | ============ | | ============ | |
| ============= ================ ================ |
=========================================================
这就是我到目前为止所做的(为了清晰起见,我删除了另一个内容):
我的问题是,我想使用高分辨率图像,并将其缩小,以匹配其旁边的最高线性布局。我的想法是让android在dpi较低的设备上缩小图像,因此在所有平台上使用相同的图像和布局
现在,主容器高度与图像高度一样大。我想要的是主容器高度与“最高”线性布局相同,并强制imageview缩小以适应主容器
有什么想法吗
谢谢
请原谅我的英语$ 尝试实现以下代码
LinearLayout ll = (LinearLayout)findViewById(R.id.your_linearlayout);
int llHeight = ll.getHeight();
ImageView iv = (ImageView)findViewById(R.id.your_imageview);
LayoutParams ivParams = ((ImageView)findViewById(R.id.your_imageview)).getLayoutParams();
ivParams.height = llHeight;
iv.setLayoutParams(ivParams );
请参见下图
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:weightSum="1"
android:background="#FFFFFF"
android:layout_height="match_parent">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
android:background="#000000"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:weightSum="5"
android:orientation="horizontal">
<ImageView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="0.9"
android:background="@drawable/black"/>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1.15">
</LinearLayout>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="0.9"
android:weightSum="4"
android:orientation="vertical">
<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="Linear Layout"
android:gravity="center_vertical|center_horizontal"
android:textSize="25dp"/>
<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="TextView"
android:gravity="center_vertical|center_horizontal"
android:textSize="25dp"/>
<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="TextView"
android:gravity="center_vertical|center_horizontal"
android:textSize="25dp"/>
<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="TextView"
android:gravity="center_vertical|center_horizontal"
android:textSize="25dp"/>
</LinearLayout>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1.15">
</LinearLayout>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="0.9"
android:weightSum="4"
android:orientation="vertical">
<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="Linear Layout"
android:gravity="center_vertical|center_horizontal"
android:textSize="25dp"/>
<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="TextView"
android:gravity="center_vertical|center_horizontal"
android:textSize="25dp"/>
<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="TextView"
android:gravity="center_vertical|center_horizontal"
android:textSize="25dp"/>
<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="TextView"
android:gravity="center_vertical|center_horizontal"
android:textSize="25dp"/>
</LinearLayout>
</LinearLayout>
所有视图的高度、宽度和所有这些内容都在活动的onMeasure()中测量。。值得注意的一点是,任何视图的onMeasure()都会在onDraw()之前执行,并且可以执行n次。因此,在其onMeasure()中计算线性布局的宽度和高度是很好的。对不起,可能是重复的。但这不会给我三个相同宽度的视图吗?线性布局是水平方向,而不是垂直方向。我希望imageview与它旁边的线性布局具有相同的高度。但我不想修改宽度。@Gonzalo Brusco为此,你必须动态地获得线性布局高度,然后设置图像视图高度。我想了一下。但如果我走这条路,我将不得不重新计算屏幕上的imageview旋转。我只是希望有一个简单的解决方案,只使用xml。类似于“将父身高设置为该子身高,而忽略其他子身高”的内容。无论如何,谢谢@尽管你的答案是最不完整的,但它是最有帮助的。其他答案对我没有要求的宽度进行了限制。根据您的回答,我想我可以创建一个自定义布局,根据另一个高度固定其高度。所以,尽管不是最好的答案,我还是选择你的答案作为公认的答案。谢谢阿披实舒克拉!你本可以用一张更好的图片来展示。。。或者至少把背景换成相反的颜色。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:weightSum="1"
android:background="#FFFFFF"
android:layout_height="match_parent">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
android:background="#000000"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:weightSum="5"
android:orientation="horizontal">
<ImageView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="0.9"
android:background="@drawable/black"/>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1.15">
</LinearLayout>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="0.9"
android:weightSum="4"
android:orientation="vertical">
<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="Linear Layout"
android:gravity="center_vertical|center_horizontal"
android:textSize="25dp"/>
<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="TextView"
android:gravity="center_vertical|center_horizontal"
android:textSize="25dp"/>
<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="TextView"
android:gravity="center_vertical|center_horizontal"
android:textSize="25dp"/>
<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="TextView"
android:gravity="center_vertical|center_horizontal"
android:textSize="25dp"/>
</LinearLayout>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1.15">
</LinearLayout>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="0.9"
android:weightSum="4"
android:orientation="vertical">
<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="Linear Layout"
android:gravity="center_vertical|center_horizontal"
android:textSize="25dp"/>
<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="TextView"
android:gravity="center_vertical|center_horizontal"
android:textSize="25dp"/>
<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="TextView"
android:gravity="center_vertical|center_horizontal"
android:textSize="25dp"/>
<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="TextView"
android:gravity="center_vertical|center_horizontal"
android:textSize="25dp"/>
</LinearLayout>
</LinearLayout>