android fragment 菜单栏,android UI:底部菜单栏的学习与制作——Fragment碎片一
碎片(Fragment) 嵌入與活動中的UI片段,為了合理的分配布局而存在,這是我的簡單理解。多用于兼顧手機與平板的UI,也適用于靈活高級的UI制作。
Demo 簡單的按鍵切換兩片不同的Demo
新建left_fragment.xml
android:id="@+id/button"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:text="Button"
/>
新建right_fragment.xml
android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:textSize="20sp"android:text="This is right fragment"/>
新建another_right_fragment.xml
android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:textSize="20sp"android:text="This is right fragment"/>
main_Activity.xml
>
android:layout_width="0dp"android:name="test.example.com.fragmenttest.LeftFragment"android:layout_height="match_parent"android:layout_weight="1"android:id="@+id/left_fragment"/>
android:id="@+id/right_layout"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1">
分別新建對應的類
packagetest.example.com.fragmenttest;importandroid.app.Fragment;importandroid.os.Bundle;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;/*** Created by hs769 on 2017/4/4.*/
public class LeftFragment extendsFragment {
@OverridepublicView onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view=inflater.inflate(R.layout.lift_fregment,container,false);returnview;
}
}
packagetest.example.com.fragmenttest;//import android.app.Fragment;
importandroid.support.v4.app.Fragment;importandroid.os.Bundle;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;/*** Created by hs769 on 2017/4/4.*/
public class RightFragment extendsFragment{
@OverridepublicView onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view=inflater.inflate(R.layout.right_fragment,container,false);returnview;
}
}
packagetest.example.com.fragmenttest;importandroid.support.v4.app.Fragment;importandroid.os.Bundle;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;/*** Created by hs769 on 2017/4/4.*/
public class AnotherRightFragment extendsFragment {
@OverridepublicView onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view=inflater.inflate(R.layout.another_right_fragment,container,false);returnview;
}
}
packagetest.example.com.fragmenttest;importandroid.support.v4.app.Fragment;importandroid.support.v4.app.FragmentManager;importandroid.support.v4.app.FragmentTransaction;importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;public class MainActivity extends AppCompatActivity implementsView.OnClickListener{
@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(this);
replaceFragment(newRightFragment());
}
@Overridepublic voidonClick(View v) {switch(v.getId()){caseR.id.button:
replaceFragment(newAnotherRightFragment());break;default:break;
}
}private voidreplaceFragment(Fragment fragment){
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction transaction=fragmentManager.beginTransaction();
transaction.replace(R.id.right_layout,fragment);
transaction.commit();
}
}
LeftFragment,RightFragment和another_Right_Fragment這三個類分別extends(繼承)Fragment類,這是一個關鍵,因為有兩個包中含有Fragment,建議選擇android.support.v4.app.Fragment
如果包選擇不一樣會出現如下錯誤(MainAcitvity.java),如圖更改即可:找到出問題的類,更換包,完畢
最終效果實現點擊button切換碎片(下圖為點擊前后的變化,分別為兩個Fragment)
總結
以上是生活随笔為你收集整理的android fragment 菜单栏,android UI:底部菜单栏的学习与制作——Fragment碎片一的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android国家坐标,从坐标获取国家?
- 下一篇: 原 Linux搭建SVN 服务器2