Java 如何在ScrollView和LinearLayout中居中
请看一下下面的代码Java 如何在ScrollView和LinearLayout中居中,java,android,scrollview,android-linearlayout,Java,Android,Scrollview,Android Linearlayout,请看一下下面的代码 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".DisplayResult"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DisplayResult" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/zodiac1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/aries"
/>
<TextView
android:id="@+id/orTxt"
android:text="Or"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/zodiac2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/gemini"
/>
</LinearLayout>
</ScrollView>
我需要把所有东西都放在线性布局的中心。我必须使用scrollview,因为当应用程序100%完成时,会有很多信息无法在不滚动的情况下一目了然地查看。如何使这些中心?请帮忙
更新
使用卷轴内的任何布局,没有问题。但是我需要这些元素的顺序相同(一个接一个)并且居中。尝试在
线性布局上使用布局重力到水平居中:
<LinearLayout
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:orientation="horizontal"/>
对于线性布局,您需要将重力设置为居中
< LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal" >
这个对我很有用:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
#如果你想在一个中心进行线性布局,你需要在你的线性布局中写入android:gravity=“center\u horizontal”。有什么方法可以编程实现吗?在创建视图时找到你的线性布局,然后在.setGravity(gravity.center)中;这是我的工作,你的答案和已经标记为答案的答案有什么不同?你能解释一下吗?另外,建议对你的解决方案做一点解释。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DisplayResult"
scrollbars="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal">
<ImageView
android:id="@+id/zodiac1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/aries"
/>
<TextView
android:id="@+id/orTxt"
android:text="Or"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/zodiac2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/gemini"
/>
</LinearLayout>
</ScrollView>