2011-12-22 32 views
2

我是刚刚接触android开发(v 4.0,API 14)并坚持实施方向更改。调整方向变化的意见

我已经添加了以下行清单文件 android:configChanges="orientation|keyboardHidden|screenSize">

但它看起来像screenSize参数没有工作,所以我不得不手工编写这两种方法:

public void ChangetoLandscape() { 
    ed.setTranslationX(300.0f); 
    btnindex.setTranslationX(300.0f); 
    btngainer.setTranslationX(300.0f); 
    btnloser.setTranslationX(300.0f); 
    lsym.setTranslationX(200.0f); 
    ltable.setTranslationX(300.0f); 
    tv1.setTranslationX(290.0f); 
    tv2.setTranslationX(300.0f); 
    tv4.setTranslationX(300.0f); 

    mySimpleXYPlot.setScaleX(3.0f); 
    mySimpleXYPlot.setScaleY(0.8f); 
    mySimpleXYPlot.setPlotMarginLeft(50.0f); 
    mySimpleXYPlot.setTranslationX(60.0f); 
    mySimpleXYPlot.setTranslationY(-70.0f); 

    sv1.setTranslationY(-80.0f); 
} 

public void ChangetoPortrait() { 

//NOTE THAT IF I DON'T WRITE BELOW CODE, 
//SWITCHING TO PORTRAIT MODE AFTER LANDSCAPE MODE RUINS THE WHOLE LAYOUT. 
     ed.setTranslationX(-1.0f); 
     btnindex.setTranslationX(-1.0f); 
     btngainer.setTranslationX(-1.0f); 
     btnloser.setTranslationX(-1.0f); 
     lsym.setTranslationX(-1.0f); 
     ltable.setTranslationX(-1.0f); 
     tv1.setTranslationX(-1.0f); 
     tv2.setTranslationX(-1.0f); 
     tv4.setTranslationX(-1.0f); 
     mySimpleXYPlot.setScaleX(1.0f); 
     mySimpleXYPlot.setScaleY(1.0f); 
     mySimpleXYPlot.setPlotMarginLeft(1.0f); 
     mySimpleXYPlot.setTranslationX(-1.0f); 
     mySimpleXYPlot.setTranslationY(-1.0f); 

     sv1.setTranslationY(-1.0f); 
} 

我使用RelativeLayout,所以我必须手动将每个视图转换到方向更改上的指定位置。

main.xml中

<RelativeLayout android:layout_width="match_parent" 
android:layout_height="match_parent" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:id="@+id/MainLayout">" 
<EditText 
    android:id="@+id/txt1" 
    android:layout_width="100dp" 
    android:layout_height="wrap_content" 
    android:hint="Search" 
    android:layout_marginLeft="220dp" 
    android:selectAllOnFocus="true" 
    android:cursorVisible="false" > 
</EditText> 

<Button 
    android:id="@+id/btnindex" 
    android:layout_width="60dp" 
    android:layout_height="wrap_content" 
    android:text="Index" 
    android:layout_marginLeft="140dp" 
    android:layout_below="@id/txt1" 
    android:textSize="12dp"/> 

<Button 
    android:id="@+id/btngainer" 
    android:layout_width="60dp" 
    android:layout_height="wrap_content" 
    android:text="Gainer" 
    android:layout_below="@id/txt1" 
    android:layout_toRightOf="@id/btnindex" 
    android:textSize="12dp"/> 

<Button 
    android:id="@+id/btnloser" 
    android:layout_width="60dp" 
    android:layout_height="wrap_content" 
    android:text="Loser" 
    android:layout_below="@id/txt1" 
    android:layout_toRightOf="@id/btngainer" 
    android:textSize="12dp"/> 

<TextView 
    android:id="@+id/tv1" 
    android:layout_width="75dp" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="150dp" 
    android:layout_marginTop="100dp" 
    android:textStyle="bold" 
    android:gravity="center" 
    android:textSize="10dp" 
    android:textColor="#00ff00" 
    android:text="SYMBOL"/> <!-- 2 similar textviews --> 

<ListView 
     android:id="@+id/ltable" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="150dp" 
     android:layout_marginTop="70dp" 
     android:layout_alignParentRight="true" 
     android:layout_width="match_parent" 
     android:layout_below="@id/txt1"> 
</ListView> 

<com.androidplot.xy.XYPlot 
android:id="@+id/mySimpleXYPlot" 
android:layout_width="140dp" 
android:layout_height="200dp" 
android:layout_alignTop="@+id/btnindex" 
title="Index"/> 

<ScrollView android:id="@+id/sv1" 
android:layout_width="300dp" 
android:layout_height="100dp" 
android:layout_marginTop="250dp"> 

<LinearLayout android:id="@+id/LinearLayout02" 
android:layout_width="wrap_content" 
android:layout_height="100dp" 
android:orientation="vertical"> 

<TextView android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:id="@+id/tv1" 
android:text="Scrollview Item 1"/> <!-- 5 similar textviews --> 
</LinearLayout> 
</ScrollView> 

</RelativeLayout> 

  1. 但作为显示在下面的截图,在横向模式下,这是使用android plot绘制了没有得到正确显示的图表。这背后的原因是我用setScaleX()方法bcoz setWidth()不可用。我不想让图表看起来很紧张。相反,它的宽度应该增加。

  2. ScrollView在portrait mode中正常显示,但在landscape mode中,根据它的高度是100dp,它不是可见的。

ScreenShot


编辑:改变android:configChanges="orientation|keyboardHidden|screenSize"android:configChanges="orientation|screenSize", 后,我得到这个当我切换到横向模式:

ScreenShot

+1

这里有几个惊人的东西。 当你说“screenSize参数不工作”时,你是什么意思?你重写OnConfigChanged()并从onConfigChanged()调用这些方法吗?如果你是,这正是应该发生的。其次,你似乎更像是使用AbsoluteLayout来使用RelativeLayout。我强烈建议你阅读RelativeLayout教程。如果正确实施,你不必做任何翻译。一切都应该照顾。 – 2011-12-22 08:56:01

+0

继续.. 第三,它看起来像你正在寻找potrait和风景模式的不同布局。如果这是真的,你应该考虑使用layout-land和layout-potrait。 – 2011-12-22 08:56:49

+0

@VikramBodicherla 1)我重写onConfigChanged(),但我调用了'ChangetoLandscape()'和'ChangetoPortrait()'方法。 **没有任何**与screenSize相关。 2.)你能简单地解释一下,我在用** RelativeLayout **做**错误**。 3)我尝试使用'layout-land'和'layout-potrait',但它**重新创建**活动,这正是我**不**想要的。 – GAMA 2011-12-22 09:30:53

回答

1

使用LayoutParams放置图在适当的位置而不是scale()功能。