我想在我的应用程序中获得滑动功能,这是一个片段

时间:2015-11-26 04:26:39

标签: android animation android-fragments slide

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

        <Button
            android:id="@+id/btnBalance"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:background="@drawable/custom_button"
            android:text="@string/btnBalance" />

        <Button
            android:id="@+id/btnMeeting"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:background="@drawable/custom_button"
            android:text="@string/btnNextMeetingDateTime" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/second"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/first"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btnBalanceTransfer"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:background="@drawable/custom_button"
            android:text="@string/btnBalanceTransfer" />

        <Button
            android:id="@+id/btnMobileTopUp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:background="@drawable/custom_button"
            android:text="@string/btnMobileTopUp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/second"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btnStatement"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:background="@drawable/custom_button"
            android:text="@string/btnStatement" />
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView"/>
    </LinearLayout>
</RelativeLayout>

这是我的布局我想在点击按钮时替换这个布局,并用另一个片段布局替换为右边的滑动效果..我可以获得功能,但只有顶部线性布局被替换,而我想要替换整个布局。我在onClick上写的代码就是这个

FragmentTransaction fragTransaction = getFragmentManager()
             .beginTransaction();
             fragTransaction.setCustomAnimations(R.anim.slide_left,
             R.anim.slide_right);
             Meeting fragMeeting = new Meeting();
             fragTransaction.replace(R.id.first, fragMeeting);
             fragTransaction.addToBackStack("test");
             fragTransaction.commit();

4 个答案:

答案 0 :(得分:0)

在您的父RelativeLayout

中加一个RelativeLayout
<RelativeLayout
    android:id="@+id/main"
    android:layout_width="fill_parent"
    android:layout_height="match_parent" >

    //your LinearLayout code

</RelativeLayout>

并在fragTransaction.replace

中提供其ID
fragTransaction.replace(R.id.main, fragMeeting);

答案 1 :(得分:0)

你用你的片段替换了 R.id.first 在哪里替换整个视图,你必须写 的 android.R.id.content

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_left,R.anim.slide_right);
Fragment fragMeeting = new Meeting();
ft.replace(android.R.id.content, fragMeeting);
ft.addToBackStack(null);
ft.commit();

如果我的回答有帮助,请将其命名为+1

答案 2 :(得分:0)

这是截屏。 Back按钮和文本视图是我想用

替换当前片段的片段

答案 3 :(得分:0)

我设法克服了片段重叠的问题,但是将背景颜色更改为白色但是出现了新问题,即点击每个标签中显示的按钮后显示的片段...我错过了任何修复或想法我能看懂吗?