避免Android在布局中将项目推离屏幕?
我有两个类似的问题,都涉及到避免Android在布局中将项目推离屏幕?,android,android-layout,android-linearlayout,android-view,android-relativelayout,Android,Android Layout,Android Linearlayout,Android View,Android Relativelayout,我有两个类似的问题,都涉及到ImageView推TextView,我不知道为什么。对于卡片列表,我希望图像的高度与其父级匹配,同时宽度适当缩放,文本左对齐,正好与图像右对齐。对于弹出窗口,使用下面的文本调整父窗口的宽度和缩放高度 我期待/寻找的是: 当前卡片布局.xml: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk
ImageView
推TextView
,我不知道为什么。对于卡片列表,我希望图像的高度与其父级匹配,同时宽度适当缩放,文本左对齐,正好与图像右对齐。对于弹出窗口,使用下面的文本调整父窗口的宽度和缩放高度
我期待/寻找的是: 当前
卡片布局.xml
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="120dp" >
<android.support.v7.widget.CardView
android:id="@+id/card_view"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_margin="10dp"
android:layout_height="120dp"
card_view:cardCornerRadius="4dp"
card_view:elevation="14dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:scaleType="fitStart"
android:padding="4dp"
android:layout_gravity="left"
android:layout_weight="1">
</ImageView>
<TextView
android:id="@+id/location"
android:textSize="16dip"
android:layout_toRightOf ="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top" >
</TextView>
<TextView
android:id="@+id/abbreviation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/location"
android:layout_toRightOf="@+id/image"
android:layout_toEndOf="@+id/image">
</TextView>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
当前位置\u info\u card.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/info_image"
android:layout_height="0dp"
android:layout_width="match_parent"
android:scaleType="fitStart"
android:padding="10dp"
android:layout_weight="1">
</ImageView>
<TextView
android:id="@+id/info_abbreviation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="4dp">
</TextView>
<TextView
android:id="@+id/info_location"
android:textSize="16sp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="4dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
我已经尝试了教程中的几种解决方案,以及单布局和嵌套布局的帖子,但到目前为止似乎没有任何效果。这只是我目前拥有的
图像视图
上使用包装内容
和文本视图
上使用布局
时,为什么会在相对视图
中将其推出屏幕关于#2,由于对类似问题的回答,所以只针对该问题发布了一个解决方案,没有任何解释,我似乎无法将其应用于我的情况。我正在寻找一个资源来了解版面和视图是如何相互作用的(单个和嵌套时),如果一个解释太长的话。关于
card\u layout.xml
版面,看起来您根本没有最顶部的文本视图的任何垂直对齐。这里有android:gravity
参数,但它与android:layout\u gravity
不同。它只设置视图内容在视图中的显示方式,而不定位视图本身。您应该添加一行像“android:layout_alignTop=“@id/image”,而不是重力
至于第二张,您已经将卡的高度设置为与父卡匹配,因此它会填满整个屏幕也就不足为奇了。您需要将所有内容(包括布局和内部元素)的高度设置为wrap\u content
,并删除权重属性,它们需要设置为所有视图才能执行任何有用的操作
还有一件重要的事。我注意到您没有在ImageView
s上指定android:adjustViewBounds
。您必须将其设置为true
,否则视图将尝试根据固有的可绘制尺寸设置比例,而不是缩放以保持纵横比
关于学习材料,我会阅读线性和相对布局的工作原理。首先,你的问题不一定与嵌套有关。现在网上有很多,我相信你自己也能找到。另外,不要害怕在Android Studio中进行实验。关于card\u layout.xml
布局,看起来最顶端的文本视图没有任何垂直对齐。这里有android:gravity
参数,但它与android:layout\u gravity
不同。它只设置视图内容在视图中的显示方式,而不定位视图本身。您应该添加一行像“android:layout_alignTop=“@id/image”,而不是重力
至于第二张,您已经将卡的高度设置为与父卡匹配,因此它会填满整个屏幕也就不足为奇了。您需要将所有内容(包括布局和内部元素)的高度设置为wrap\u content
,并删除权重属性,它们需要设置为所有视图才能执行任何有用的操作
还有一件重要的事。我注意到您没有在ImageView
s上指定android:adjustViewBounds
。您必须将其设置为true
,否则视图将尝试根据固有的可绘制尺寸设置比例,而不是缩放以保持纵横比
关于学习材料,我会阅读线性和相对布局的工作原理。首先,你的问题不一定与嵌套有关。现在网上有很多,我相信你自己也能找到。也不要害怕在Android Studio中进行实验。您应该使用嵌套布局和权重属性:)对于弹出窗口,尝试删除ImageView
上的layout\u weight
,并将其layout\u height
设置为wrap\u content
。关于卡片,我在本地尝试了您的布局,它对我有效。只是确认一下,文本视图是否确实可见,是否使用了除白色以外的字体颜色,并以编程方式正确设置了它们应该显示的文本?@bufforba0t是的,如果我更改布局类型/缩放比例,使所有项目都显示在同一行上,然后,每张卡都可以看到正确的文本。您应该使用嵌套布局和权重属性:)对于弹出窗口,尝试删除ImageView
上的layout\u weight
,并将其layout\u height
设置为wrap\u content
。关于卡,我在本地尝试了您的布局,它对我有效。只是确认一下,文本视图是否确实可见,使用的字体颜色不是白色,并且以编程方式正确设置了它们应该显示的文本?@choorba0t是的,如果我更改布局类型/缩放比例,使所有项目都显示在同一行上,那么每张卡都可以看到正确的文本。谢谢,虽然我还是不太明白。也许还有一个更具体的问题:为什么当我有一个ImageView,其中包含高度和宽度的wrap_内容和scaleType:fitStart嵌套在一个在某个点上具有固定大小的布局中时,ImageView的右侧是否有一个巨大的空间?我本以为它会将图像缩放到最大高度,然后有一个最大值