Android 如何自动调整宽度和高度?
我正在用Android Studio制作应用程序,希望自动设置我在头版显示的图像的宽度和高度。我的代码是这样的Android 如何自动调整宽度和高度?,android,android-layout,Android,Android Layout,我正在用Android Studio制作应用程序,希望自动设置我在头版显示的图像的宽度和高度。我的代码是这样的 <HorizontalScrollView android:layout_width="wrap_content" android:layout_height="wrap_content"> <LinearLayout android:layout_width="wrap_content" a
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:id="@+id/linearLayout1"
android:orientation="vertical"
android:layout_height="fill_parent">
<TextView
android:id="@+id/textView"
android:layout_width="375dp"
android:layout_height="220dp"
android:layout_marginBottom="5sp"
android:adjustViewBounds="true"
android:background="@drawable/japuji"
android:clickable="true"
android:focusable="true"
android:linksClickable="true"
android:scaleType="fitXY"
android:textStyle="bold" />
这是scrollview,当我在TextView中设置fill_parent时,wrap_内容也不起作用,当我在大屏幕图像上测试它时
我尝试过改变宽度来填充父对象和包装内容,但文本视图的高度和宽度都是硬编码的:
android:layout\u width=“375dp”android:layout\u height=“220dp”
相反,将TextView的高度和宽度设置为
match_parent
您显然是在TextView中显示图像(android:background=“@drawable/japuji”
),但是TextView的高度和宽度是硬编码的:android:layou width=“375dp”android:layou height=“220dp”
相反,将TextView的高度和宽度设置为
匹配\u parent
您不应将图像显示为视图的背景,而应使用ImageView
<ImageView
android:contentDescription="@string/imagedesc"
android:id="@+id/my_image"
android:src="@drawable/your_image"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
src:设置可绘制的图像
scaleType:设置所需的缩放类型,在这种情况下,fitCenter将adjustViewBounds设置为true,并将width或height(其中一个)设置为wrap\u内容将显示完整图像
编辑:
如果您有一个固定的视图大小,并且希望用可绘制的图像覆盖所有视图,则应使用centerCrop作为scaleType而不是fitCenter您不应将图像显示为视图的背景,而应使用ImageView
<ImageView
android:contentDescription="@string/imagedesc"
android:id="@+id/my_image"
android:src="@drawable/your_image"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
src:设置可绘制的图像
scaleType:设置所需的缩放类型,在这种情况下,fitCenter将adjustViewBounds设置为true,并将width或height(其中一个)设置为wrap\u内容将显示完整图像
编辑:
如果您有一个固定的视图大小,并且希望用可绘制的图像覆盖所有视图,那么您应该使用centerCrop作为scaleType,而不是fitCenter您发布的代码中没有图像,因此我们不知道出了什么问题。除非您指的是TextView,否则在这种情况下,为什么要使用TextView而不是ImageView来显示图像?scaleType之类的东西只对ImageView起作用。“我尝试过改变宽度来填充父元素和包装内容”改变哪个元素的宽度?你发布的代码中没有图像,所以我们不知道出了什么问题。除非您指的是TextView,否则在这种情况下,为什么要使用TextView而不是ImageView来显示图像?scaleType之类的东西只在ImageView上起作用。“我尝试过改变宽度来填充父元素和包装内容”改变哪个元素的宽度?定义“不起作用”:结果是什么,它与您想要的有什么不同?定义“不起作用”:结果是什么,它与您想要的有什么不同?