RelativeLayout不适合视图中的所有内容

时间:2012-04-20 07:23:21

标签: android android-layout relativelayout

在尝试向对话框添加SeekBar时,我意识到我需要TextView反映setProgress() SeekBar。我这样实现了它:

    private void customDialogTimeout() {
    LinearLayout ll = new LinearLayout(getSherlockActivity());
    ll.setOrientation(LinearLayout.VERTICAL);

    RelativeLayout input = new RelativeLayout(getSherlockActivity());

    final SeekBar timeoutSeekBar = new SeekBar(getSherlockActivity());
    timeoutSeekBar.setId(1);
    final TextView seekBarStatus = new TextView(getSherlockActivity());
    seekBarStatus.setId(2);

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
         LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams lay1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    lay1.addRule(RelativeLayout.CENTER_VERTICAL);
    lay1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

    RelativeLayout.LayoutParams lay2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    lay2.addRule(RelativeLayout.CENTER_VERTICAL);
    lay2.addRule(RelativeLayout.RIGHT_OF, timeoutSeekBar.getId());

    layoutParams.setMargins(30, 20, 30, 0);

    input.addView(timeoutSeekBar,lay1);
    input.addView(seekBarStatus, lay2);

    ll.addView(input, layoutParams);

然而,结果视图似乎“推出”了TextView

enter image description here

我做错了什么?如果我接近这个错误的方式,请告诉我。

1 个答案:

答案 0 :(得分:1)

您的SeekBar的宽度设置为RelativeLayout.LayoutParams.MATCH_PARENT,当您向左添加TextView时,它显然不会显示,因为它已被推出屏幕(如{ {1}}已填满整个宽度):

SeekBar