我试图在 RelativeLayout 中并排显示两个按钮,然后将其添加到 LinearLayout,但结果是两个按钮像这样彼此重叠:
这是我的代码:
DisplayMetrics displaymetrics = new DisplayMetrics();
(getActivity()).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
LinearLayout ll = (LinearLayout) getActivity().findViewById(R.id.music_layout);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
lp.setMargins(Gravity.CENTER, 10, Gravity.CENTER, 10);
JSONObject[] jsons = new JSONObject[arrays.size()];
arrays.toArray(jsons);
Button [] play = new Button[jsons.length];
Button [] stop = new Button[jsons.length];
Button [] song_name = new Button[jsons.length];
for(int i =0; i < jsons.length; i++){
play[i] = new Button(getActivity());
stop[i] = new Button(getActivity());
song_name[i] = new Button(getActivity());
String track_name= "error", url= "error";
try {
track_name = jsons[i].getString("track_name");
url = jsons[i].getString("track_link");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
song_name[i].setText(track_name);
play[i].setText("Play");
stop[i].setText("Stop");
song_name[i].setGravity(Gravity.LEFT);
play[i].setGravity(Gravity.CENTER);
stop[i].setGravity(Gravity.CENTER);
song_name[i].setTextColor(getActivity().getResources().getColor(R.color.white));
play[i].setTextColor(getActivity().getResources().getColor(R.color.white));
stop[i].setTextColor(getActivity().getResources().getColor(R.color.white));
song_name[i].setBackgroundResource(R.drawable.button_shape);
play[i].setBackgroundResource(R.drawable.button_shape);
stop[i].setBackgroundResource(R.drawable.button_shape);
ll = (LinearLayout) getActivity().findViewById(R.id.music_layout);
lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
lp.setMargins(Gravity.CENTER, 10, Gravity.CENTER, 10);
ll.addView(song_name[i], lp);
RelativeLayout rl2 = new RelativeLayout(getActivity());
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
width/2,
RelativeLayout.LayoutParams.MATCH_PARENT);
lp2.addRule(RelativeLayout.LEFT_OF, stop[i].getId());
lp2.setMargins(0, 5, Gravity.CENTER, 10);
rl2.addView(play[i], lp2);
RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(
width/2,
RelativeLayout.LayoutParams.MATCH_PARENT);
lp3.addRule(RelativeLayout.ALIGN_RIGHT);
lp3.setMargins(0, 5, Gravity.CENTER, 10);
rl2.addView(stop[i], lp3);
ll.addView(rl2, lp);
}
请问我哪里出错了?
最佳答案
考虑到“向左”意味着一个 View 的开始是相对布局的左边缘..并且布局是从左到右书写的,所以...
与其说“在 rl2 的左边缘启动 lp2”……这就是您得到的结果,不如说“在左 View 结束时启动右 View ”。
所以不是这个:
lp2.addRule(RelativeLayout.LEFT_OF, stop[i].getId());
假设右 p 2 在左 p 2 的右边:
rl2.addRule(RelativeLayout.RIGHT_OF, id);
如果您有 xml 布局,下一个示例将在第二个 TextView 的右侧显示第二个 TextView 。也许它有帮助:
<TextView
android:id="@+id/characteristicDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_toRightOf="@id/characteristicImage"
android:text="@string/no_description"
android:textSize="18sp" />
<TextView
android:id="@+id/characteristicUuid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/characteristicDescription"
android:layout_marginLeft="8dp"
android:layout_toRightOf="@id/characteristicImage"
android:text="@string/_"
android:textColor="#ffffff"
android:textSize="14sp" />
关于java - Android - RelativeLayout - 并排按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24834856/