2011-08-22 149 views
1

我在android中有一個活動,我在SurfaceView下設置了一個按鈕。結果看起來是這樣的: 在android中加載動畫按鈕

而且我對它滿意,除了一件事,按鈕上的文字是錯誤的,必須正確設置。 爲此,我使用的動畫是這樣的:

takePhoto = (Button) findViewById(R.id.btnPhoto); 
takePhoto.setText(t); 

RotateAnimation ranim = (RotateAnimation)AnimationUtils.loadAnimation(this, R.anim.myanim); 
ranim.setFillAfter(true); 
takePhoto.setAnimation(ranim); 

其中res/anim/myanim樣子:

<?xml version="1.0" encoding="utf-8"?> 
<rotate xmlns:android="http://schemas.android.com/apk/res/android" 
     android:fromDegrees="0" 
     android:toDegrees="-90" 
     android:pivotX="50%" 
     android:pivotY="50%" 
     android:duration="0" /> 

但結果有點令人失望,導致整個看起來像:

enter image description here

文字沒問題,但尺寸縮小了。

我想要的是保持正確的文本和初始size.How我可以做到這一點? 它無關的xml文件的原因按鈕的屬性加載動畫....想法時,我並沒有改變他們謝謝...

編輯:xml file

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

<SurfaceView android:layout_width="450dip" 
android:layout_height="fill_parent" android:id="@+id/surface_camera" /> 

<RelativeLayout android:layout_width="fill_parent" 
android:layout_toRightOf="@id/surface_camera" 
android:layout_height="fill_parent" 
> 

<Button android:layout_width="680dip" 
android:layout_height="680dip" 
android:id="@+id/btnPhoto" 
android:layout_alignParentBottom="true" 
android:text="Take Photo" 
android:layout_gravity="bottom|right" /> 

</RelativeLayout> 

</RelativeLayout> 





IMPORTANT: 

In the cases described below...my activity is in `LANDSCAPE` mode and the position of the phone is portrait.Another issue is that when I turn my phone in landscape mode(and here I mean the position of the phone, cause the activity is still landscape) the button doesn't go at the bottom of the phone is stays there.No matter how I turn the phone the button is in the same position!!!! 

回答

2

你應該閱讀this

取2個佈局文件夾喜歡如下...

enter image description here

在這兩個文件夾的佈局文件名應該是相同的。

layout-port文件夾佈局文件應該是這樣的......

android:layout_toRightOf="@id/surface_camera" 

換成...

android:layout_Below="@id/surface_camera" 

如下...

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

<SurfaceView android:layout_width="450dip" 
android:layout_height="fill_parent" android:id="@+id/surface_camera" /> 

<RelativeLayout android:layout_width="fill_parent" 
android:layout_toBelow="@id/surface_camera" 
android:layout_height="fill_parent" 
> 

<Button android:layout_width="680dip" 
android:layout_height="680dip" 
android:id="@+id/btnPhoto" 
android:layout_alignParentBottom="true" 
android:text="Take Photo" 
android:layout_gravity="bottom|right" /> 

</RelativeLayout> 

</RelativeLayout> 

即使是這樣將被認爲是不好的做法,但你也可以手動處理這個,試試這個..

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //setContentView(R.layout.main); 

     int orientation = getResources().getConfiguration().orientation; 

     if(orientation == Configuration.ORIENTATION_PORTRAIT) { 
     setContentView(R.layout.main1); 

     } 
     else { 

     setContentView(R.layout.main2); 
     } 
    } 

此外,如果你正在做這樣沒有必要做兩個佈局文件夾,但需要兩個佈局文件MAIN1和MAIN2在同一個文件夾

另一個選擇......

清單中。 ..

<activity android:name=".ActivityName" android:configChanges="keyboardHidden|orientation"> 

+

在你的活動......

@Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 

     int orientation = newConfig.orientation; 
     if(orientation == Configuration.ORIENTATION_PORTRAIT) { 

     setContentView(R.layout.main2); 
     } 
     else { 

     setContentView(R.layout.main); 
     } 
    } 
0

嘗試這個XML代替:

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

    <SurfaceView android:id="@+id/surface_camera" 
        android:layout_width="450dip" 
        android:layout_height="fill_parent" /> 

    <FrameLayout android:layout_width="fill_parent" 
        android:layout_toRightOf="@id/surface_camera" 
        android:layout_height="fill_parent"> 
     <Button  android:layout_width="match_parent" 
        android:layout_height="match_parent"/> 
     <TextView android:id="@+id/btnPhoto" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:text="Take Photo" /> 
    </FrameLayout> 

    </RelativeLayout> 

您將旋轉文本而不是按鈕。