2012-01-11 7 views
1

Я создал виджет 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> 

ответ

2

Если вы хотите этот макет портретной и альбомной тогда вы пишете жесткий код на OnCreate, как это: - Там будет время, вам нужно определить свой графический интерфейс приложений как в альбомной и портретной (просто повернуть телефон) ,

К счастью, каждый дроид имеет встроенную функцию для определения типа макета. Что вам нужно реализовать:

  1. Создайте два файла main.xml для раскладки в обоих случаях: пейзаж и портрет.

  2. Поместите эти два main.xml ресурсов в соответствии с его типом:

    /res/layout-land/main.xml

    /res/layout-port/main.xml

Просто создайте и попробуйте графический интерфейс приложения во время выполнения.

В случае, если вы хотите, чтобы обнаружить выбор времени, когда изменение типа макета, и вы хотите сделать некоторые дополнительные работы в кодировании, вы можете хотите выполнить следующие шаги:

  1. Добавить андроид: configChanges =»ориентацию» к 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

+0

Вместо двух макетов, один для портрета и один для ландшафта вы можете иметь один parameterzied расположение (например, управление прокладкой или другие атрибуты) и получить параметры из res/values ​​и res/values ​​- относиться к земле с уважением. Таким образом, вам не нужно повторять общие aspeptcs двух макетов. – user1076637

0

При изменении ориентации экрана он снова начнется с onCreate(). Таким образом, вы можете проверить, какова ваша текущая ориентация экрана в onCreate(), а затем динамически устанавливать высоту и ширину вашего представления. сохраните свой xml-код, поскольку он динамически задает высоту ширины вашего представления, когда он находится в ландшафте.