1

In my android app, I want to create a linear layout horizontally and add a textview on the left side and a progress bar on right right side, I want the textview to wrap content for width. And the progress bar should (for width) take up the remaining space horizontally on the screen.

This is my code:

        LinearLayout.LayoutParams percentWidth = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

        LinearLayout percentLayout = new LinearLayout(this);
        percentLayout.setOrientation(LinearLayout.HORIZONTAL);
        percentLayout.setLayoutParams(percentWidth);

        textview = new TextView(this);
        textview.setText(String.format("Tasks Completed: %d%%", personobj.percentage));
        percentLayout.addView(textview);

        ProgressBar checklistprogress = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
        checklistprogress.setProgress(personobj.percentage);
        percentLayout.addView(checklistprogress);

        LocationLayout.addView(percentLayout);

However this is not working, the progress bar width is not expanding fully. It's only about a centimeter width.

Does anyone know how to do this?

Thanks.

4

3 に答える 3

0

この種のものには RelativeLayout を使用することをお勧めします。layout_weightただし、線形レイアウトを使用する場合は、属性を使用する必要があります。layout_weight を使用すると、複数のビュー間のサイズ比を指定できます。以下のコードでは、最後のパラメーターはレイアウトの重みです。

LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
                             LayoutParams.MATCH_PARENT,
                             LayoutParams.MATCH_PARENT, 1.0f);

layout_weight をよりよく理解するには、この記事を ご覧ください http://www.chess-ix.com/blog/the-use-of-layout_weight-with-android-layouts/

于 2013-08-27T04:15:53.193 に答える
0

使ってみてください

 checklistprogress.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
于 2013-08-27T04:17:49.963 に答える
0

@omega: このコードを試すことができます。プログレスバーのスタイルを大きく変更するために使用されます。

ProgressBar checklistprogress = new ProgressBar(this, null, android.R.attr.progressBarStyleLarge);
于 2013-08-27T04:19:07.350 に答える