Android - RelativeLayout - 并排按钮

时间:2014-07-18 22:45:44

标签: java android android-linearlayout relativelayout

我试图在RelativeLayout中并排显示两个按钮,然后将其添加到LinearLayout,但结果是两个按钮在彼此的顶部,如下所示:

enter image description here

这是我的代码:

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);                
        }

我哪里出错了?

2 个答案:

答案 0 :(得分:1)

您正在为两个视图lp2使用相同的布局参数对象。添加播放和停止按钮时,需要创建单独的布局参数对象。例如,您应该有lpPlaylpStop

答案 1 :(得分:1)

考虑到“向左”意味着一个视图的开头是相对布局的左边缘..布局是从左到右书写的......

而不是说“在rl2的左边缘开始lp2”......这就是你得到的,说“当左视图结束时开始正确的视图”。

所以不要这样:

lp2.addRule(RelativeLayout.LEFT_OF, stop[i].getId());

说右p 2位于左侧p 2的右侧:

 rl2.addRule(RelativeLayout.RIGHT_OF, id);

如果您有xml布局,下一个示例会显示第二个文本视图右侧的第二个文本视图..也许有帮助:

<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" />