Java 中心图像视图
我一直在从线性布局和相对布局切换,因为我想达到图像的中心Java 中心图像视图,java,android,android-linearlayout,android-relativelayout,Java,Android,Android Linearlayout,Android Relativelayout,我一直在从线性布局和相对布局切换,因为我想达到图像的中心 <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/scrollView1" android:scrollbars="none" android:layout_width="fill_parent"
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:scrollbars="none"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/itemItem"
android:src="@drawable/content_picture"
android:tag="image_item_grid_image"
android:layout_width="wrap_content"
android:background="@drawable/layout_bg"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center|center_horizontal"
android:adjustViewBounds="true"
android:contentDescription="Desc"
android:scaleType="fitCenter" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout >
</ScrollView>
由于某种原因,图像没有居中,我尽我所知尝试了很多,但似乎没有
试试这个
移除gravity添加android:layout\u centerHorizontal=“true”
和android:layout\u centerVertical=“true”
试试这个
移除gravity添加android:layout\u centerHorizontal=“true”
和android:layout\u centerVertical=“true”
您的相对布局设置为:
<RelativeLayout
android:layout_width="wrap_content" // will not fill parent so children will centre in relative layout, not the whole view
android:layout_height="wrap_content"
android:orientation="vertical" >
您应该将相对布局设置为填充父视图的宽度和高度,或者将滚动视图布局设置为重心,以便相对布局在滚动视图中居中。您的相对布局设置为:
<RelativeLayout
android:layout_width="wrap_content" // will not fill parent so children will centre in relative layout, not the whole view
android:layout_height="wrap_content"
android:orientation="vertical" >
您应该将相对布局设置为填充父视图的宽度和高度,或者将滚动视图布局设置为重心,以便相对布局在滚动视图中居中。尝试使用
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
尝试使用
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
尝试为父ScrollView设置重力。尝试为父ScrollView设置重力。仍不工作。尝试了您的说明。我还在底部使用了layoutParams
part@lordzden使用RelativeLayout
而不是ScrollView
我将其替换为您的代码,图像仍如图所示。我对布局的处理是否会以编程方式影响xml?请查看layoutParams代码
。也许可以尝试删除layoutParams代码,看看它是否解决了问题。我不明白为什么它会影响它。addrule会起作用!!!哦,天哪,我没看到它来了。您可以分享addrule工作的原因吗?:)还是不工作,先生。尝试了您的说明。我还在底部使用了layoutParams
part@lordzden使用RelativeLayout
而不是ScrollView
我将其替换为您的代码,图像仍如图所示。我对布局的处理是否会以编程方式影响xml?请查看layoutParams代码
。也许可以尝试删除layoutParams代码,看看它是否解决了问题。我不明白为什么它会影响它。addrule会起作用!!!哦,天哪,我没看到它来了。您可以分享addrule工作的原因吗?:)我确实尝试在scrollview上添加重心,但图像仍然没有居中,顺便说一下,我在零件上有布局参数。请看。你也尝试过添加布局图重力,这与重力不同。我尝试过在scrollview上添加重心,但是图像没有居中,顺便说一下,我在零件上有布局参数。请看。你也尝试过添加重力布局,这与重力不同。
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"