【问题标题】:How to rotate an image button to match the screen orientation?如何旋转图像按钮以匹配屏幕方向?
【发布时间】:2015-09-29 01:23:30
【问题描述】:

我有一个包含多个图像和切换按钮的布局。如何让它们旋转以匹配屏幕方向?我没有旋转布局,只是这些元素。示例:屏幕为纵向,按钮完全垂直对齐。设备旋转并切换到横向,因此按钮旋转以适应用户的观点。怎样才能达到这样的效果?度数旋转动画如何不仅与它所影响的元素相关联,而且还与方向传感器相关联?

【问题讨论】:

    标签: android android-layout android-orientation


    【解决方案1】:

    你必须设置“android:gravity to center”。 以下布局将为您提供帮助:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:gravity="center"
                  android:orientation="horizontal" >
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cool Button" />
    
    </LinearLayout>
    

    还有其他方法可以做到。

    1. 使用相对布局
    2. 使用 2 种布局:一种用于纵向,另一种用于横向。

    使用两种布局的示例: 纵向模式的 XML 文件(可在 res/layout 文件夹中找到)

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button  
            android:id="@+id/Apple"  
            android:text="Apple"
            android:layout_width="300dp" 
            android:layout_height="wrap_content" 
            android:padding="20dip"
            android:layout_marginTop="20dip" />
        <Button  
            android:id="@+id/Mango"  
            android:text="Mango"
            android:layout_width="300dp" 
            android:layout_height="wrap_content" 
            android:padding="20dip"
            android:layout_marginTop="20dip" />
        <Button  
            android:id="@+id/Banana"  
            android:text="Banana"
            android:layout_width="300dip" 
            android:layout_height="wrap_content"   
            android:padding="20dip" 
            android:layout_marginTop="20dip"  />
        <Button  
            android:id="@+id/Grapes"  
            android:text="Grapes"
            android:layout_width="300dip" 
            android:layout_height="wrap_content"    
            android:padding="20dip"
            android:layout_marginTop="20dip"   />
        <Button  
            android:id="@+id/Kiwi"  
            android:text="Kiwi"
            android:layout_width="300dip" 
            android:layout_height="wrap_content"    
            android:padding="20dip"
            android:layout_marginTop="20dip"  />
    </LinearLayout>
    

    我们可以看到五个Button控件垂直排列在一个LinearLayout容器中,一个在另一个之下。这种垂直排列使一些 Button 控件在屏幕处于横向模式时消失。

    要在横向模式下使用屏幕右侧的空白区域,我们需要在 res/layout-land 文件夹中定义另一个布局文件 activity_screen_orientation_app.xml。 layout-land 文件夹必须在 res 文件夹中手动创建。右键单击 Package Explorer 窗口中的 res 文件夹,然后选择 New, Folder 选项。将打开一个对话框,询问新文件夹的名称。将名称 layout-land 分配给新文件夹,然后单击 Finish 按钮。

    横向模式的 XM 文件(可在 res/layout-land 文件夹中找到)

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button  
            android:id="@+id/Apple"  
            android:text="Apple"
            android:layout_width="250dp" 
            android:layout_height="wrap_content" 
            android:padding="20dip"
            android:layout_marginTop="20dip" />
        <Button  
            android:id="@+id/Mango"  
            android:text="Mango"
            android:layout_width="250dp" 
            android:layout_height="wrap_content" 
            android:padding="20dip"
            android:layout_marginTop="20dip" 
            android:layout_toRightOf="@id/Apple" />
        <Button  
            android:id="@+id/Banana"  
            android:text="Banana"
            android:layout_width="250dip" 
            android:layout_height="wrap_content"   
            android:padding="20dip" 
            android:layout_marginTop="20dip" 
            android:layout_below="@id/Apple" />
        <Button  
            android:id="@+id/Grapes"  
            android:text="Grapes"
            android:layout_width="250dip" 
            android:layout_height="wrap_content"    
            android:padding="20dip"
            android:layout_marginTop="20dip"  
            android:layout_below="@id/Apple"
            android:layout_toRightOf="@id/Banana"  />
        <Button  
            android:id="@+id/Kiwi"  
            android:text="Kiwi"
            android:layout_width="250dip" 
            android:layout_height="wrap_content"    
            android:padding="20dip"
            android:layout_marginTop="20dip" 
            android:layout_below="@id/Banana" />
    </RelativeLayout>
    

    我们还可以通过 Java 代码检测屏幕方向。让我们修改 Activity 文件 ScreenOrientationAppActivity.java 以在屏幕在横向模式和纵向模式之间切换时显示一个 toast 消息。

    package com.androidunleashed.screenorientationapp;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.Toast;
    
    public class ScreenOrientationAppActivity extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_screen_orientation_app);
       if(getResources().getDisplayMetrics().widthPixels>getResources().getDisplayMetrics().
                heightPixels) 
            {  
                Toast.makeText(this,"Screen switched to Landscape mode",Toast.LENGTH_SHORT).show(); 
            } 
            else 
            { 
                Toast.makeText(this,"Screen switched to Portrait mode",Toast.LENGTH_SHORT).show(); 
            }
        }
    }
    

    【讨论】:

    • 他不想为风景制作另一种布局。旋转屏幕,但按钮应根据传感器旋转。但布局应该保留。
    • 所以他必须使用另一种方式,即使用相对布局
    • 另一个布局没有问题,它是android文档的标准,但如果你仍然不想创建另一个,那么你应该使用相对布局。
    猜你喜欢
    • 2017-09-17
    • 1970-01-01
    • 2011-10-11
    • 2021-07-15
    • 2018-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多