【问题标题】:Widget size decreasing while changing the orientation改变方向时小部件尺寸减小
【发布时间】:2012-01-11 14:09:40
【问题描述】:

我创建了一个 WiFi 小工具。在将方向从纵向更改为横向时,它的大小正在减小。 我正在使用的布局如下所示。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent" >

<LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_gravity="center" >

<ImageView
    android:id="@+id/wifi_background"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    />

<!--<TextView
    android:id="@+id/wifi_status"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:layout_marginTop="5dip"
    android:layout_marginLeft="35dip"
    android:layout_gravity="center_horizontal" />

 -->
</LinearLayout>
 <TextView
    android:id="@+id/wifi_off"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="15dip"
    android:layout_marginLeft="25dip"
    android:textColor="@android:color/white"
    android:textSize="15dip"
     />
<TextView
    android:id="@+id/wifi_notconnected"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:layout_marginTop="43dip"
    android:layout_marginLeft="35dip"
    android:layout_gravity="center_horizontal"
    android:textSize="14dip" />
 <TextView
    android:id="@+id/wifi_connecting"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:layout_marginTop="8dip"
    android:layout_marginLeft="33dip"
    android:layout_gravity="center_horizontal"
    android:textSize="14dip" />
<TextView
    android:id="@+id/wifi_obtaining"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="10dip"
    android:layout_marginLeft="40dip"
    android:textColor="@android:color/white"
    android:textSize="10dip"
    />
 <TextView
    android:id="@+id/wifi_on"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="10dip"
    android:layout_marginLeft="40dip"
    android:textColor="@android:color/white"
    android:textSize="10dip"
     />

<ImageView
    android:id="@+id/wifi_signal_strength"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<FrameLayout android:layout_width="wrap_content"
  android:layout_height="wrap_content">

<RelativeLayout 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content" 
    android:layout_marginTop="5dip">

 <ImageView
    android:id="@+id/wifi_circle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/onpress_network_info_null"
    android:src="@drawable/toggle"
    />

   <ImageView
        android:layout_alignRight="@+id/wifi_circle"
        android:id="@+id/wifi_square"
        android:layout_width="85dip"
        android:layout_height="60dip" 
        android:background="@drawable/network_info_null"
        android:src="@drawable/togglenetwork"/>

     </RelativeLayout>
    </FrameLayout>
 </FrameLayout>

【问题讨论】:

    标签: android layout widget


    【解决方案1】:

    如果您想要纵向和横向两种布局,那么您可以在 oncreate 上编写如下硬代码:- 有时您需要以横向和纵向定义应用程序 GUI(只需旋转手机)。

    幸运的是,每个机器人都有内置功能来检测布局类型。你需要实现的是:

    1. 为两种情况下的布局创建两个 main.xml 文件:横向和纵向。

    2. 将这两个main.xml根据类型放入资源中:

      /res/layout-land/main.xml

      /res/layout-port/main.xml

    只需在运行时构建并试用您的应用程序 GUI。

    如果您想检测布局类型更改的时间,并且想在编码中做一些额外的工作,您可能需要按照以下步骤操作:

    1. 将 android:configChanges=”orientation” 添加到 AndroidManifest.xml

    2. 检测方向变化:

    @Override

    public void onConfigurationChanged(Configuration newConfig) {
      Configuration c = getResources().getConfiguration();
    
      if(c.orientation == Configuration.ORIENTATION_PORTRAIT ) {
        // portrait
    
      } else if(c.orientation == Configuration.ORIENTATION_LANDSCAPE ){
        // landscape
    
      }   }
    

    Details

    如果你只想要肖像,那么

    AndroidManifest.xml, declare your Activity like so: <activity ...    android:screenOrientation="portrait" .../>
    

    如果你只想要风景,那么

     AndroidManifest.xml, declare your Activity like so: <activity ...     android:screenOrientation="landscape" .../>
    

    如果您想了解更多信息,请访问这些链接

    http://developer.android.com/resources/articles/faster-screen-orientation-change.html

    http://developer.android.com/guide/topics/manifest/activity-element.html#screen

    StackOverflow search

    【讨论】:

    • 您可以使用单一参数化布局(例如控制填充或其他属性)并从 res/values 和 res/values-land 获取参数,而不是两种布局,一种用于纵向,一种用于横向分别。这样你就不需要重复这两种布局的共同点了。
    【解决方案2】:

    当屏幕方向改变时,它会再次从 onCreate() 开始。因此,您可以在 onCreate() 中检查您当前的屏幕方向,然后动态设置视图的高度和宽度。保留您的 xml 代码,因为它仅在 landsacpe 中动态设置视图的高度宽度。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-24
      • 2021-11-25
      • 2012-10-05
      • 2018-01-30
      • 2012-04-22
      • 2012-02-17
      相关资源
      最近更新 更多