Android 动态设置布局时,相对布局的元素固定在左上角
我在一个片段文件中动态地编码了相对布局和其他元素。尽管我尝试了Android 动态设置布局时,相对布局的元素固定在左上角,android,android-fragments,android-relativelayout,Android,Android Fragments,Android Relativelayout,我在一个片段文件中动态地编码了相对布局和其他元素。尽管我尝试了.addRule(RelativeLayout.ALIGN\u PARENT\u BOTTOM) 和.addRule(RelativeLayout.down,id) 我觉得addRule根本不会影响我的代码 有人能帮我吗 该活动包含以下片段: Fragment fragment = new RadioButtonFragment(); fragment.setArguments(args); ft.replace(R.id.fragm
.addRule(RelativeLayout.ALIGN\u PARENT\u BOTTOM)
和.addRule(RelativeLayout.down,id)
我觉得addRule
根本不会影响我的代码
有人能帮我吗
该活动包含以下片段:
Fragment fragment = new RadioButtonFragment();
fragment.setArguments(args);
ft.replace(R.id.fragment_container1,fragment);
ft.commit();
<?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"/>
该活动的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;
}
}
公共类RadioButtonFragment扩展片段{
公共无线电按钮碎片(){
//必需的空公共构造函数
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
d(“onCreateFragment”,“调用onCreate”);
视图v=充气机。充气(R.layout.fragment_单选按钮,容器,错误);
RelativeLayout RelativeLayout=v.findViewById(R.id.RelativeLayout单选按钮);
if(relativeLayout==null)
Log.d(“relativeLayoutError”,“relativeLayout为空”);
字符串[]选项={“choice1”、“choice2”、“choice3”、“choice4”、“choice5”};
RelativeLayout.LayoutParams relativeParams=新的RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_父级,
RelativeLayout.LayoutParams.MATCH_父项
);
relativeLayout.setLayoutParams(relativeParams);
//文本视图
TextView questionTextView=新的TextView(getActivity());
RelativeLayout.LayoutParams textViewParams=新的RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_父级,
RelativeLayout.LayoutParams.WRAP_内容
);
questionTextView.setText(“这是问题的文本”);
textViewParams.addRule(RelativeLayout.ALIGN\u PARENT\u TOP);
questionTextView.setLayoutParams(textViewParams);
int qViewId=1;
questionTextView.setId(qViewId);
relativeLayout.addView(问题文本视图);
//选择题
//创建滚动视图
ScrollView sv=新的ScrollView(getActivity());
//创建单选按钮组
RadioGroup rg=新的RadioGroup(getActivity());
rg.设置方向(线性布局。垂直);
整数=5;
//i=1是问题,2是上一个按钮,3是下一个按钮
对于(int i=4;i
片段的XML文件:
Fragment fragment = new RadioButtonFragment();
fragment.setArguments(args);
ft.replace(R.id.fragment_container1,fragment);
ft.commit();
<?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"/>
谢谢。将重力设置为LayoutParams对象,如下所示:
bottomRelativeLayout.gravity = Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL;