我已经在Fragment文件中动态编码了相对布局和其他元素。我一直在努力解决每个元素都卡在左上角的问题,即使我尝试了.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)
和.addRule(RelativeLayout.BELOW , id)
。
我觉得addRule
根本不影响我的代码。
有人可以帮我吗?
活动包含片段:
Fragment fragment = new RadioButtonFragment();
fragment.setArguments(args);
ft.replace(R.id.fragment_container1,fragment);
ft.commit();
该活动的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_container">
<FrameLayout
android:id="@+id/fragment_container1"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
我正在谈论的片段:
public class RadioButtonFragment extends Fragment {
public RadioButtonFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("onCreateFragment", "onCreate is called.");
View v = inflater.inflate(R.layout.fragment_radio_button, container, false);
RelativeLayout relativeLayout = v.findViewById(R.id.relativelayout_radio_button);
if(relativeLayout == null)
Log.d("relativeLayoutError", "relativeLayout is null");
String[] choices = {"choice1", "choice2", "choice3", "choice4", "choice5"};
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT
);
relativeLayout.setLayoutParams(relativeParams);
// text view
TextView questionTextView = new TextView(getActivity());
RelativeLayout.LayoutParams textViewParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
questionTextView.setText("this is the text of the question");
textViewParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
questionTextView.setLayoutParams(textViewParams);
int qViewId = 1;
questionTextView.setId(qViewId);
relativeLayout.addView(questionTextView);
// for multiple choice
// create scroll view
ScrollView sv = new ScrollView(getActivity());
// make radio button group
RadioGroup rg = new RadioGroup(getActivity());
rg.setOrientation(LinearLayout.VERTICAL);
int number = 5;
// i = 1 is question, 2, is previous button and 3 is next button
for (int i = 4; i < number + 4; i++) {
RadioButton rdbtn = new RadioButton(getActivity());
rdbtn.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 300));
rdbtn.setId(i);
rdbtn.setText(choices[i-4]);
rg.addView(rdbtn);
}
sv.addView(rg);
RelativeLayout.LayoutParams radioGroupViewParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
radioGroupViewParams.addRule(RelativeLayout.BELOW, questionTextView.getId());
relativeLayout.addView(sv);
// set on click method
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton radioButton = getView().findViewById(checkedId);
Toast.makeText(getActivity(), radioButton.getText(), Toast.LENGTH_LONG).show();
}
});
RelativeLayout bottomRelativeLayout = new RelativeLayout(getActivity());
RelativeLayout.LayoutParams buttomRelativeParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
buttomRelativeParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
relativeLayout.setLayoutParams(buttomRelativeParams);
relativeLayout.addView(bottomRelativeLayout);
// create previous button
Button previousButton = new Button(getActivity());
RelativeLayout.LayoutParams previousButtonParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.MATCH_PARENT
);
previousButtonParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
previousButton.setText("previous");
bottomRelativeLayout.addView(previousButton);
// create next button
Button nextButton = new Button(getActivity());
RelativeLayout.LayoutParams nextButtonParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.MATCH_PARENT
);
nextButtonParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
nextButton.setText("next");
bottomRelativeLayout.addView(nextButton);
Log.d("endOfAdd radio", "endOfAdd radio");
return v;
}
}
片段的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/relativelayout_radio_button"/>
谢谢。
答案 0 :(得分:0)
将重力设置为LayoutParams对象,如下所示:
bottomRelativeLayout.gravity = Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL;