Android 布局重力
我正在尝试创建一个布局,其中在纵向模式下,layout_gravity 应该为 center_vertical,而当我处于横向模式时,它应该位于顶部。
当我在横向模式下使用 center_vertical 时,布局位于中心,我可以向上滚动。
纵向
景观
我如何在单个布局文件中执行此操作,而不需要多个布局文件。
布局代码:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/splash_background_gradient" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingTop="20dip"
android:paddingBottom="20dip"
android:orientation="vertical" >
<ImageView
android:id="@+id/SplashLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="40dip"
android:src="@drawable/splash_logo" />
<Button
android:id="@+id/FacebookLogin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:background="@drawable/splash_facebook_button_selector"
android:gravity="center"
android:padding="0dip"
android:text="@string/splash_facebook"
android:textColor="#FFFFFF"
android:textSize="8pt"
android:textStyle="bold" />
<TextView
android:id="@+id/SplashSignupLabel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="28dp"
android:layout_marginRight="28dp"
android:layout_marginTop="20dip"
android:text="@string/splash_signup_label"
android:textColor="#e9e9e9"
android:textSize="6pt"
android:textStyle="bold" />
<Button
android:id="@+id/SplashSignupButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:background="@drawable/login_button_selector"
android:text="@string/splash_signup"
android:textStyle="bold" />
<TextView
android:id="@+id/SplashLoginLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="28dp"
android:layout_marginRight="28dp"
android:layout_marginTop="20dip"
android:text="@string/splash_login_label"
android:textColor="#e9e9e9"
android:textSize="6pt"
android:textStyle="bold" />
<Button
android:id="@+id/SplashLoginButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:background="@drawable/login_button_selector"
android:text="@string/login"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
I am trying to create a layout where when in Portrait mode the layout_gravity should be center_vertical and when i am in Landscape mode it should be top.
When i have center_vertical in landscape mode the layout is positioned to the center and i can scroll up..
Portrait
Landscape
How can i do this in a single layout file and not having multiple ones.
Layout code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/splash_background_gradient" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingTop="20dip"
android:paddingBottom="20dip"
android:orientation="vertical" >
<ImageView
android:id="@+id/SplashLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="40dip"
android:src="@drawable/splash_logo" />
<Button
android:id="@+id/FacebookLogin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:background="@drawable/splash_facebook_button_selector"
android:gravity="center"
android:padding="0dip"
android:text="@string/splash_facebook"
android:textColor="#FFFFFF"
android:textSize="8pt"
android:textStyle="bold" />
<TextView
android:id="@+id/SplashSignupLabel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="28dp"
android:layout_marginRight="28dp"
android:layout_marginTop="20dip"
android:text="@string/splash_signup_label"
android:textColor="#e9e9e9"
android:textSize="6pt"
android:textStyle="bold" />
<Button
android:id="@+id/SplashSignupButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:background="@drawable/login_button_selector"
android:text="@string/splash_signup"
android:textStyle="bold" />
<TextView
android:id="@+id/SplashLoginLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="28dp"
android:layout_marginRight="28dp"
android:layout_marginTop="20dip"
android:text="@string/splash_login_label"
android:textColor="#e9e9e9"
android:textSize="6pt"
android:textStyle="bold" />
<Button
android:id="@+id/SplashLoginButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:background="@drawable/login_button_selector"
android:text="@string/login"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从资源的“layout”文件夹中复制布局并将其粘贴到“layout-land”文件夹中,然后在第二个文件夹中更改所需的设置。
Copy your layout from "layout" folder of resources and paste it in the "layout-land" folder and change those settings you need in second one.
您在横向模式下正确查看,您必须创建一个名为layout-land的新布局文件夹,并根据适合您的应用程序的外观应用元素的新定位。
you proper look in landscape mode ,you have to create a new layout folder named layout-land and apply new positioning of element according to that which looks good for your app.