我正在尝试处理Android视图及其切换。看来这应该很简单,但是......
我正在使用ADK。我创建了一个项目,ViewsToAKill,带有片段的空白活动'。我把名字保留为默认名称。然后我添加了一个名为" NextActivity"。
的新片段活动我没有触及Activity java类。其余的在下面。问题是这个。当我点击Go按钮时,我希望"再见!"取代"你好!"。相反,我看到"你好!"和Go按钮,我也看到了#34; Goodbye!"部分隐藏在按钮下方。为什么呢?
$ cat app/src/main/res/layout/activity_main.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment"
android:name="com.rrk.viewstoakill.MainActivityFragment"
tools:layout="@layout/fragment_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</fragment>
$
$ cat app/src/main/res/layout/fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivityFragment">
<TextView
android:id="@+id/hello_view"
android:text="Hello!"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button_go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/hello_view"
android:text="Go!"
android:clickable="true"
/>
</RelativeLayout>
$
$ cat app/src/main/res/layout/activity_next.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment"
android:name="com.rrk.viewstoakill.NextActivityFragment"
tools:layout="@layout/fragment_next"
android:layout_width="match_parent"
android:layout_height="match_parent" />
$
$
$ cat app/src/main/res/layout/fragment_next.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.rrk.viewstoakill.NextActivityFragment">
<TextView
android:text="Goodbye!"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
$
$ cat app/src/main/java/com/rrk/viewstoakill/MainActivityFragment.java
package com.rrk.viewstoakill;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
/**
* A placeholder fragment containing a simple view.
*/
public class MainActivityFragment extends Fragment {
public MainActivityFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView button = (Button)rootView.findViewById(R.id.button_go);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Fragment nextFragment = new NextActivityFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, nextFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
return rootView;
}
}
$
$ cat app/src/main/java/com/rrk/viewstoakill/NextActivityFragment.java
package com.rrk.viewstoakill;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A placeholder fragment containing a simple view.
*/
public class NextActivityFragment extends Fragment {
public NextActivityFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_next, container, false);
}
}
$
答案 0 :(得分:1)
基本上去
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/fragment"
android:name="com.rrk.viewstoakill.MainActivityFragment"
tools:layout="@layout/fragment_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
activity_main.xml
中的
答案 1 :(得分:0)
您是否在片段的xml布局文件中设置了background
属性?
android:background="?android:windowBackground"