目前,在运行我的应用程序时,转到此活动不会显示我的片段。我是否错误地添加了片段,因此片段中没有显示任何内容?或者片段onCreateView
方法中的动态行为是否有问题?
public class AskQuestionActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ask_question);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="@string/header_text"/>
<fragment android:name="com.mypackage.AskQuestionFragment"
android:id="@+id/question_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
public class AskQuestionFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
String questionText = "What is your favorite color?";
List<String> answers = Arrays.asList("Red", "Blue", "Yellow");
LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.question, container);
Resources res = getResources();
int id = res.getIdentifier("question_text", "id", layout.getContext().getPackageName());
TextView questionTextView = (TextView)layout.findViewById(id);
questionTextView.setText(questionText);
id = res.getIdentifier("question_answers", "id", layout.getContext().getPackageName());
RadioGroup radioGroup = (RadioGroup)layout.findViewById(id);
Collections.reverse(answers);
for (String s : answers) {
RadioButton button = new RadioButton(layout.getContext());
button.setText(s);
radioGroup.addView(button, 0);
}
return layout;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/question_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/question_text"/>
<RadioGroup android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/question_answers"></RadioGroup>
<Button
android:id="@+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/submit_button_text"
/>
</LinearLayout>
答案 0 :(得分:0)
尝试在xml中放置framelayout而不是fragment。并在您的活动的onCreate中创建要显示的片段实例并将其添加到片段容器中
公共类AskQuestionActivity扩展了FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ask_question);
AskQuestionFragment fragment = new AskQuestionFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.id_of_your_container, fragment).commit();
}
}
答案 1 :(得分:0)
执行此操作时可能出现问题:
LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.question, container);
考虑用以下之一替换该行:
LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.question, null);
或
LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.question, container, false);
答案 2 :(得分:0)
将您的布局从线性更改为framelayout,并将您的片段附加到活动中。
getSupportFragmentManager().beginTransaction()
.add(R.id.your_fragment_container, fragment).commit();