我的應用程序在縱向模式,TextSwitcher,自定義視圖和自定義鍵盤中有三個視圖。 在橫向模式下,我需要刪除TextSwitcher並在右側添加一個ListView。 我是新來的android,我可以定義兩個佈局,即xxx和xxx-land,我的問題是如何實現代碼的兩個分支,一個用於控制風景,一個用於肖像。如何在android中爲縱向和橫向模式定義不同的控件
0
A
回答
1
定義,在這兩種資源的文件出現在你的代碼 並在onCreate()
在所有視圖中Activity
可以檢查方向綁定的意見,你的類的對象。
示例。
在這裏我們有包含在這兩個文件的ImageView的,並在畫像文件有一個TextView
和景觀它包含Button
代替TextView
my_layout.xml在佈局2個佈局文件-land
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/abs__ab_bottom_solid_dark_holo" />
</LinearLayout>
my_layout.xml在佈局端口文件夾
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/abs__ab_bottom_solid_dark_holo" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
這裏是活動代碼
package com.example.stackoverflow;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Display;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MyActivity extends Activity {
// landscape views
Button button1;
// protrati views
TextView textView1;
// common views (shared between landscape and protrait mode)
ImageView imageView1;
public MyActivity() {
// TODO Auto-generated constructor stub
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
imageView1 =(ImageView) findViewById(R.id.imageView1);//exist inside landscape file and portrait
if(getScreenOrientation() == Configuration.ORIENTATION_PORTRAIT){
textView1 = (TextView)findViewById(R.id.textView1);
}else{
button1 = (Button) findViewById(R.id.button1);
}
//when you want to access any thing that is not shared
//check the orientation
}
@Override
protected void onResume() {
super.onResume();
//let say that we want here to set a text on the textview and it's available only for protrait
if(getScreenOrientation() == Configuration.ORIENTATION_PORTRAIT){
//won't be null :) so we can set the text
textView1.setText("Hello Guys!");
}
}
// http://stackoverflow.com/a/6236110/671676
public int getScreenOrientation() {
Display getOrient = getWindowManager().getDefaultDisplay();
int orientation = Configuration.ORIENTATION_UNDEFINED;
if (getOrient.getWidth() == getOrient.getHeight()) {
orientation = Configuration.ORIENTATION_SQUARE;
} else {
if (getOrient.getWidth() < getOrient.getHeight()) {
orientation = Configuration.ORIENTATION_PORTRAIT;
} else {
orientation = Configuration.ORIENTATION_LANDSCAPE;
}
}
return orientation;
}
}
問什麼,你不明白。
3
設置一個標誌,基於當前方向
getResources().getConfiguration().orientation
和訪問textswitcher或ListView檢查標誌...並根據標誌採取行動,例如在縱向模式下進行更改Textswitcher之前,但在景觀不要因爲這將是空...
相關問題
- 1. 如何爲縱向和橫向定義不同的佈局?
- 2. 爲縱向和橫向定義了不同的佈局?
- 3. 方向縱向和橫向模式
- 4. UIScrollView在縱向模式和橫向模式下的行爲不同
- 5. UISplitViewController中的ActionSheet在縱向模式中的行爲與橫向模式不同
- 6. Android:如何處理縱向反轉縱向和橫向反轉橫向事件
- 7. Android定製主題:針對橫向和縱向模式的不同主題
- 8. 如何在android中控制webview橫向和縱向?
- 9. iPad - 用於橫向和縱向模式的不同設計
- 10. Nexus 7 - 用於縱向和橫向模式的不同佈局
- 11. IOS縱向和橫向模式
- 12. 設置不同的初始規模爲橫向和縱向
- 13. Android小部件不同的縱向和橫向
- 14. UISplitViewController出現在橫向模式縱向
- 15. Android相機在縱向和橫向模式下保存圖像
- 16. 如何在android中以編程方式控制橫向和縱向?
- 17. 在縱向和橫向模式下爲iPad設置不同約束
- 18. 的XCode:如何更改的橫向和縱向模式
- 19. Android的橫向和縱向模式變化
- 20. 如何檢查appdelegate上的橫向和縱向模式?
- 21. 縱向和橫向居中
- 22. 如何從縱向模式更改爲橫向模式並鎖定它?
- 23. 如何在橫向和縱向模式下開始相同的活動?
- 24. 如何在縱向和橫向模式下在單個列表視圖中顯示不同的列數android?
- 25. 定位:縱向和橫向在Xcode 4.2
- 26. 不同的橫向和縱向設計android
- 27. 爲縱向和橫向模式調整UIImage的大小
- 28. 加載不同的縱向/橫向UIViews
- 29. 如何將縱向視圖更改爲橫向模式?
- 30. 如何在代碼中處理Android縱向和橫向?
感謝您的回覆。我現在可以繼續。 –