android界面切換
❶ android 怎麼設置鎖屏界面可以橫豎屏切換
Android橫豎屏要解決的問題應該就兩個:
一.布局問題
二.重新載入問題
1.布局問題:如果不想讓軟體在橫豎屏之間切換,最簡單的辦法就是在項目的AndroidManifest.xml中找到你所指定的activity中加上android:screenOrientation屬性,他有以下幾個參數:
"unspecified":默認值 由系統來判斷顯示方向.判定的策略是和設備相關的,所以不同的設備會有不同的顯示方向.
"landscape":橫屏顯示(寬比高要長)
"portrait":豎屏顯示(高比寬要長)
"user":用戶當前首選的方向
"behind":和該Activity下面的那個Activity的方向一致(在Activity堆棧中的)
"sensor":有物理的感應器來決定。如果用戶旋轉設備這屏幕會橫豎屏切換。
"nosensor":忽略物理感應器,這樣就不會隨著用戶旋轉設備而更改了("unspecified"設置除外)。
也可以在Java代碼中通過setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)來設置。
如果要讓軟體在橫豎屏之間切換,由於橫豎屏的高寬會發生轉換,有可能會要求不同的布局。可以通過以下方法來切換布局:
1)在res目錄下建立layout-land和layout-port目錄,相應的layout文件不變,比如main.xml。layout-land是橫屏的layout,layout-port是豎屏的layout,其他的不用管,模擬器會自動尋找。
2)通過 this.getResources().getConfiguration().orientation來判斷當前是橫屏還是豎屏然後來載入相應的 xml布局文件。因為當屏幕變為橫屏的時候,系統會重新呼叫當前Activity的onCreate方法,你可以把以下方法放在你的onCreate中來檢查當前的方向,然後可以讓你的setContentView來載入不同的layout xml.
1 if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
2 Log.i("info","landscape"); // 橫屏
3 }
4 else if(this.getResources().getConfiguration().orientation ==Configuration.ORIENTATION_PORTRAIT) {
5 Log.i("info","portrait"); // 豎屏
6 }
在onConfigurationChanged()方法中也可以檢測擁有硬鍵盤的鍵盤狀態
1 //檢測實體鍵盤的狀態:推出或者合上
2 if (newConfig.hardKeyboardHidden ==Configuration.HARDKEYBOARDHIDDEN_NO){
3 //實體鍵盤處於推出狀態,在此處添加額外的處理代碼
4 }
5 else if(newConfig.hardKeyboardHidden ==Configuration.HARDKEYBOARDHIDDEN_YES){
6 //實體鍵盤處於合上狀態,在此處添加額外的處理代碼
7 }
2.重新載入問題。如果不需要從新載入,可以在AndroidManifest.xml中加入配置 android:configChanges="orientation|keyboardHidden",配置 android:configChanges的作用就是如文檔所說的:Specify one or more configuration changesthat the activity will handle itself. If not specified, the activity will berestarted if any of these configuration changes happen in the system。這樣在程序中Activity就不會重復的調用onCreate()甚至不會調用onPause、onResume.只會調用一個 onConfigurationChanged(Configuration newConfig)。如果需要重新載入,則不需要做任何修改。不過如果需要在重新載入過程中保存之前的操作內容或數據,則需要保存之前的數據。然後在 activity的onCreate()中取出來。當然,如此就不能設置android:configChanges()了,否則就不會調用 onCreate()方法。
如果要徹底禁止翻轉,可以設置android:screenOrientation的屬性為nosensor,如此就可以忽略重力感應帶來的麻煩了。不過在模擬器上不管用,在真機上是正確的。android:screenOrientation="portrait"
則無論手機如何變動,擁有這個屬性的activity都將是豎屏顯示。
android:screenOrientation="landscape",為橫屏顯示。
這里提一個小知識,Android模擬器中,快捷鍵"Ctrl+F11/F12"可以實現轉屏
❷ Android使用fragment實現底部導航欄切換界面
源碼鏈接
效果圖
創建bottom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_width="36dp"
android:layout_height="36dp"
android:id="@+id/bottom_icon"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#565656"
android:textSize="10sp"
android:id="@+id/bottom_text" />
public void setNormalImage(int normalIcon){
this.normalIcon = normalIcon;
ivIcon.setImageResource(normalIcon);
}
public void setFocusedImage(int focusedIcon){
this.focusedIcon = focusedIcon;
}
public void setTvText(String text){
tvText.setText(text);
}
public void setFocused(boolean isFocused){
this.isFocused = isFocused;
if(isFocused){
ivIcon.setImageResource(focusedIcon);
tvText.setTextColor(Color.parseColor("#02b5bc"));
}else{
ivIcon.setImageResource(normalIcon);
tvText.setTextColor(Color.BLACK);
}
}
<FrameLayout
android:id="@+id/frameLayout_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="11"
>
</FrameLayout>
四個如下
<com.example.qiaolulu.qiaofragment.BottomLayout
android:id="@+id/square"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
</com.example.qiaolulu.qiaofragment.BottomLayout>
public class Babyextends Fragment{
@Nullable
@Override
public ViewonCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//載入你設計要顯示的界面
View view = inflater.inflate(R.layout.baby,null);
return view;
}
}
fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction =fragmentManager.beginTransaction();
firstPage =new FirstPage();
transaction.add(R.id.frameLayout_container,firstPage);
transaction.commit();
❸ android 界面切換黑屏怎麼處理
從A切換到B的過程中出現黑屏,可以在Manifest文件中改變B的theme,在theme里添加<item name="android:windowIsTranslucent">true</item>,這樣從A到B的過程中,因為B是透明的,所以背景就是A。這樣的用戶體驗比較好。
❹ android中如何實現界面切換
android中一個界面就是一個Activity,最簡單的一個界面切換就是利用intent。比如從界面A跳轉到B
Intent intent=new Intent(A,B.class);
startActivity(intent);
後面最後在加一行代碼
finishActivity(A);這是把當前的Activity 結束掉。如果界面太多,這樣會亂套。不細說了!
❺ android應用程序如何實現界面跳轉
你先寫一個xml文件 內容是<Button xmlns:android="http://scehmas.android.com/apk/res/android" android:layout_widht="wrap_content" android:layout_height="wrap_content" android:text="按鈕" android:id="@+/btn"/>
然後再第一個activity 中通過findViewById()得到這個button button.setOnClickListener(new OnClickListener(){
public void onClick(View view){
Intent intent=new Intent();
intent.setCass(Activity1.this,activity2.class)
startAtivity(intent);
});
ok 這樣就行了 純手敲望採納。