以编程方式将ProgressBar添加到Android中的片段

以编程方式将ProgressBar添加到Android中的片段

本文介绍了以编程方式将ProgressBar添加到Android中的片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够操纵Fragment中现有TextView的某些文本.但是,我无法以编程方式向现有布局添加新的 ProgressBar .
在Fragment类中:

I was able to manipulate some text of an existing TextView in a Fragment. However I was not able to programatically add a new ProgressBar to the existing layout.
In the Fragment class:

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_completed_office_hours, container, false);

        LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.linearLayoutCompletedOfficeHours);

        progressBar = new ProgressBar(this.getContext());
        progressBar.setMax(daysInTotal);
        progressBar.setProgress(daysCompleted);

        linearLayout.addView(progressBar);

        TextView textView = (TextView) view.findViewById(R.id.completedXOfYDays);
        textView.setText(daysCompleted + " / " + daysInTotal);
        return view;
    }

xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragment.CompletedOfficeHoursFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/activity_horizontal_margin"
        android:orientation="horizontal"
        android:id="@+id/linearLayoutCompletedOfficeHours">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/completedXOfYDays" />
    </LinearLayout>
</FrameLayout>

执行它时,我得到 19/299 作为文本,但没有任何 ProgressBar .我在做什么错了=

When executing it I get 19 / 299 as text, but without any ProgressBar. What am I doing wrong=

推荐答案

您已使用 android:orientation ="horizo​​ntal" 指定了"linearLayoutCompletedOfficeHours"作为线性布局,并指定了textview Android:layout_width ="match_parent" .这样做会使textview占据整个空间,并且会创建进度条并将其显示在屏幕中.而是将textView的宽度更改为 android:layout_width ="wrap_content" ,进度将是可见的.

You have specified "linearLayoutCompletedOfficeHours" as a Linear Layout with android:orientation="horizontal" and given textview android:layout_width="match_parent".Making this will make textview to take entire space and progressbar is created and shown in the screen.Instead change textView width to android:layout_width="wrap_content" and the progress will be visible.

<TextView
   android:layout_width="wrap_content"
   android:layout_height="match_parent"
   android:id="@+id/completedXOfYDays" />

这篇关于以编程方式将ProgressBar添加到Android中的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 03:51