2016-10-03 28 views
1

我试图创建用户界面的登录页面与徽标两个编辑文本和登录按钮,事情是非常好的手机肖像模式,但是当我检查它在景观和平板电脑,然后问题开始了。Android布局创建问题,而从相对和线性布局从纵向切换

我有两个解决方案

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/relativelayoutparent" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#fed2db" 
    android:orientation="vertical" 
    > 
     <RelativeLayout 
      android:id="@+id/linearlayout_parents" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

      <RelativeLayout 
       android:id="@+id/relativelayoutbasic" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="@dimen/margin20" 
       android:layout_marginRight="@dimen/margin20" 
       android:layout_centerInParent="true" 
       > 
       <EditText 
        android:id="@+id/userName" 
        android:inputType="text" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="[email protected]" 
        android:hint="UserName" 
        android:drawableStart="@mipmap/ic_launcher" 
        android:drawablePadding="10dp" 
        android:background="@null" 
        android:layout_marginTop="10dp" /> 
       <View 
        android:id="@+id/view" 
        android:layout_width="match_parent" 
        android:layout_height="1dp" 
        android:background="#f7f7f7" 
        android:layout_below="@id/userName" 
        android:layout_marginTop="10dp" /> 
       <EditText 
        android:id="@+id/password" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_below="@id/view" 
        android:inputType="textPassword" 
        android:hint="Password" 
        android:text="[email protected]" 
        android:drawablePadding="10dp" 
        android:background="@null" 
        android:drawableStart="@mipmap/ic_launcher" 
        android:layout_marginTop="10dp" /> 
       <Button 
        android:id="@+id/buttonLogin" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_below="@id/password" 
        android:background="#f7f7f7" 
        android:text="Login" 
        android:textColor="@android:color/black" 
        android:textAllCaps="false" 
        android:textStyle="bold" 
        android:textSize="17sp" 
        android:layout_marginTop="10dp" /> 
       <Button 
        android:id="@+id/buttonForgotPassword" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_below="@id/buttonLogin" 
        android:layout_marginTop="10dp" 
        android:text="Forgot Password ?" 
        android:textColor="@android:color/white" 
        android:textAllCaps="false" 
        android:background="@null" /> 
      </RelativeLayout> 

      <ImageView 
       android:id="@+id/imageview" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@mipmap/ic_launcher" 
       android:layout_marginTop="@dimen/margin20" 
       android:layout_above="@id/relativelayoutbasic" 
       android:layout_centerHorizontal="true"/> 
     </RelativeLayout> 
</RelativeLayout> 

这部作品在纵向模式完美,但正如我们在景观旋转它减小图像。

显示在下面的画像例子 enter image description here

下面显示在风景enter image description here

所以我尝试下一个解决方案

2.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/relativelayoutparent" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#3996db" 
    android:orientation="vertical" 
    > 

    <ScrollView 
     android:id="@+id/linearlayout_parentscroll" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     <LinearLayout 
      android:id="@+id/linearlayout_parents" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

      <ImageView 
       android:id="@+id/imageview" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@mipmap/ic_launcher" 
       android:layout_marginTop="@dimen/margin20" 
       android:layout_gravity="center_horizontal"/> 

      <RelativeLayout 
       android:id="@+id/relativelayoutbasic" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="@dimen/margin20" 
       android:layout_marginRight="@dimen/margin20" 

       > 
       <EditText 
        android:id="@+id/userName" 
        android:inputType="text" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="[email protected]" 
        android:hint="UserName" 
        android:drawableStart="@mipmap/ic_launcher" 
        android:drawablePadding="10dp" 
        android:background="@null" 
        android:layout_marginTop="10dp" /> 
       <View 
        android:id="@+id/view" 
        android:layout_width="match_parent" 
        android:layout_height="1dp" 
        android:background="#f7f7f7" 
        android:layout_below="@id/userName" 
        android:layout_marginTop="10dp" /> 
       <EditText 
        android:id="@+id/password" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_below="@id/view" 
        android:inputType="textPassword" 
        android:hint="Password" 
        android:text="[email protected]" 
        android:drawablePadding="10dp" 
        android:background="@null" 
        android:drawableStart="@mipmap/ic_launcher" 
        android:layout_marginTop="10dp" /> 
       <Button 
        android:id="@+id/buttonLogin" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_below="@id/password" 
        android:background="#f7f7f7" 
        android:text="Login" 
        android:textColor="@android:color/black" 
        android:textAllCaps="false" 
        android:textStyle="bold" 
        android:textSize="17sp" 
        android:layout_marginTop="10dp" /> 
       <Button 
        android:id="@+id/buttonForgotPassword" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_below="@id/buttonLogin" 
        android:layout_marginTop="10dp" 
        android:text="Forgot Password ?" 
        android:textColor="@android:color/white" 
        android:textAllCaps="false" 
        android:background="@null" /> 
      </RelativeLayout> 
     </LinearLayout> 
    </ScrollView> 

</RelativeLayout> 

A中的例子。在此解决方案中,我们无法像使用相对布局一样使用属性android:layout_centerInParent =“true” ,因此我们无法像在解决方案一中那样使登录文本字段居中,该解决方案在小屏幕中看起来不错,有滚动也不能在大屏幕上工作,因为它不会在中心。

B.我们无法在scrollview中使用相对布局,因为它会产生很多其他复杂情况。

我花了这么多时间,使用这么多的堆栈溢出引用和其他人也没有工作。

请提出一些方法来处理它。 在此先感谢...

+0

你的问题是什么? – LightYearsBehind

回答

1

这里的问题是你使用@mipmap/ic_launcher图像的宽度/高度"wrap_content"
所以ImageView规模将立足于ic_launcher图像尺寸
ic_launcher图像尺寸是解构不是肖像小
=>您ImageView横向比纵向小

因此,简单的设置为您ImageView宽度,那么你的图像尺寸看起来在纵向和横向相同

<ImageView 
    android:id="@+id/imageview" 
    android:layout_width="100dp" // or whatever you want 
    android:layout_height="100dp" 
    android:src="@mipmap/ic_launcher" 
    android:layout_marginTop="@dimen/margin20" 
    android:layout_gravity="center_horizontal"/> 
+0

如果我将添加宽度,那么对于不同的设备它将保持不变,并且在每个设备上都不会很好。还有一件事我试图把更大尺寸的图像放在风景上,但它没有奏效。 –

+0

@RohitChaturvedi你知道如何使用dimen吗?尝试使用为你的图像宽度创建不同的维度。此外,你也可以创建更多的布局文件为特定的屏幕大小 –

+0

是的,这是一个很好的建议,我完全错过了使用维。我相信这一定会起作用。如果它不能解决问题,我会进一步发帖。感谢Phan Van Linh –

0

您可以创建一个L尺寸指定ayout -land或甚至layout-sw600dp-land适用于平板电脑。

+0

是的,我可以工作,但我只想用一种布局工作。 - –

1

在横向模式下打开应用程序时,Android会首先检查是否存在特定于横向的任何布局,如果是,则加载它而不是在布局文件夹中指定的布局。要做到这一点,你需要创建一个新的文件夹RES /布局土地,然后从布局文件夹复制布局到这个文件夹,然后对其进行编辑,并一道亮丽的风景布局。

+0

我完全同意你的观点,这种方式也可以工作,但我只想使用一种布局。 –