Android 调整图像大小以适应父布局的高度和宽度
我想根据我的Android 调整图像大小以适应父布局的高度和宽度,android,android-layout,imageview,scaletype,Android,Android Layout,Imageview,Scaletype,我想根据我的RelativeLayout的高度和宽度来拟合图像视图,而不影响其比率。我使用了一个带有weightSum的LinearLayout,并添加了另外两个布局,即带有weight20的RelativeLayout和带有weight80的LinearLayout。我正在将imageview添加到RelativeLayout以占据权重20,但目前,我的图像占据了其内容的宽度,并且不遵循父布局的宽度,从而导致推送其他布局 我的布局如下: layout.xml <?xml version=
RelativeLayout
的高度和宽度来拟合图像视图
,而不影响其比率。我使用了一个带有weightSum
的LinearLayout
,并添加了另外两个布局,即带有weight
20的RelativeLayout
和带有weight
80的LinearLayout
。我正在将imageview
添加到RelativeLayout
以占据权重
20,但目前,我的图像占据了其内容的宽度,并且不遵循父布局的宽度,从而导致推送其他布局
我的布局如下:
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/order_detail_container"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="8dp"
android:layout_marginTop="16dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="1dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="100"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20">
<Droid.CenterFitImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:id="@+id/imgArticleThumb"
android:src="@drawable/Icon" />
</RelativeLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="80"
android:padding="5dp"
android:orientation="vertical">
<Droid.CustomTextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/txtCategory"
card_view:customFont="Fonts/Roboto-Bold.ttf"
android:textColor="@color/CatTitle"
android:textSize="12sp"
android:text="Category" />
<Droid.CustomTextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/txtTitle"
android:ellipsize="end"
android:singleLine="true"
card_view:customFont="Fonts/Roboto-Bold.ttf"
android:textColor="#000000"
android:textSize="16sp"
android:text="Title" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
预期产出:
我的输出:
用maxwidth替换图像的layout\u width=“0”
。用maxwidth替换图像的layout\u width=“0”
。刚使用的android:layout\u height=“wrap\u content”代替ImageView的匹配父项,并设置文本视图高度和宽度wrap\u内容。刚使用的android:layout\u height=“换行内容”而不是ImageView的匹配父对象,并设置文本视图的高度和宽度换行内容。首先,为什么不使用Linearlayout而不是RelativeLayout。当您使用所有Linearlayout时
我会这样做的
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/order_detail_container"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="8dp"
android:layout_marginTop="16dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="1dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="100"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="40"
android:gravity="center">
<Droid.CenterFitImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:id="@+id/imgArticleThumb"
android:src="@drawable/Icon" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="60"
android:orientation="vertical"
android:layout_marginLeft="5dp">
<Droid.CustomTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtCategory"
card_view:customFont="Fonts/Roboto-Bold.ttf"
android:textColor="@color/CatTitle"
android:textSize="12sp"
android:text="Category" />
<Droid.CustomTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtTitle"
android:ellipsize="end"
android:singleLine="true"
card_view:customFont="Fonts/Roboto-Bold.ttf"
android:textColor="#000000"
android:textSize="16sp"
android:text="Title" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
首先,当您使用所有Linearlayout时,为什么没有使用Linearlayout而不是RelativeLayout
我会这样做的
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/order_detail_container"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="8dp"
android:layout_marginTop="16dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="1dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="100"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="40"
android:gravity="center">
<Droid.CenterFitImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:id="@+id/imgArticleThumb"
android:src="@drawable/Icon" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="60"
android:orientation="vertical"
android:layout_marginLeft="5dp">
<Droid.CustomTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtCategory"
card_view:customFont="Fonts/Roboto-Bold.ttf"
android:textColor="@color/CatTitle"
android:textSize="12sp"
android:text="Category" />
<Droid.CustomTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtTitle"
android:ellipsize="end"
android:singleLine="true"
card_view:customFont="Fonts/Roboto-Bold.ttf"
android:textColor="#000000"
android:textSize="16sp"
android:text="Title" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
根直线上的布局\u width=“wrap\u content”是您出错的地方。如果您需要根据视图的权重正确对齐视图,则必须使用匹配父视图
请记住,如果使用布局权重和权重总和,则必须根据排列给出根布局和特定的宽度或高度。换行内容意味着根据子级的大小显示布局
因此,生成的代码将是
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="100"
android:orientation="horizontal">
....
....
</LinearLayout>
....
....
这是我得到的结果
第一个与ImageView的相对值为20,另一个与TextView的相对值为80。根LinaerLayout\u width=“wrap\u content”上的布局是错误的地方。如果需要根据视图的权重正确对齐视图,则必须使用匹配父视图
请记住,如果使用布局权重和权重总和,则必须根据排列给出根布局和特定的宽度或高度。换行内容意味着根据子级的大小显示布局
因此,生成的代码将是
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="100"
android:orientation="horizontal">
....
....
</LinearLayout>
....
....
这是我得到的结果
第一个与ImageView的相对值为20,另一个与TextView的相对值为80。是否要设置两个视图的50%,50%。android:layout\u weight=“80”
和android:layout\u weight=“20”替换如何“
分别为50,50%。是否要将两个视图的50%和50%都设置。?将android:layout\u weight=“80”
和android:layout\u weight=“20”
分别替换为50,50。我想在那里添加更多组件。在这种情况下,使用相对而不是线性是否会有所不同?是否要在哪个布局中添加更多组件?我根据需要使用线性布局或相对布局。在易于使用的地方线性布局我为什么要选择相对性我想在那里添加更多的组件。在这种情况下,使用相对而不是线性是否会有所不同?是否要在哪个布局中添加更多组件?我根据需要使用线性布局或相对布局。在易于使用的地方线性布局为什么我应该选择相对性如果您能准确指定要替换的一个(因为有两个用“0dp”)以及解决问题的原因,这会很有帮助。如果您能准确指定要替换的一个(因为有两个用“0dp”),这将解决问题的原因。