如何相对于tablelayout中的另一个视图添加视图(例如循环进度条)

时间:2015-07-08 20:58:50

标签: java android android-studio

希望有人可以提供建议。

我在Android工作室中创建了一个Android应用程序,它动态地以编程方式将大量按钮添加到tablelayout中。用户可以设置按钮的数量,因此可以有1个或者可以有100个。使用tablelayout,它们可以整齐地添加到每行的两个按钮,并且大小正确。我很高兴。

我想在每个按钮前面添加另一个视图(以圆形进度条的形式),这样当按下按钮时,圆圈会旋转,而无论设置按钮的操作是什么。我希望进度条最好位于按钮的中间,但我并不过分挑剔。

我可以就实现这一目标的最佳方法获得一些建议吗?我需要在相对布局中重做我的按钮吗?有没有办法在表格布局中的其他视图上添加视图?任何建议或建议欢迎。

谢谢

我当前的按钮代码......

(假设每行有两个按钮,你知道行数和按钮数)。

   TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);
   TableRow.LayoutParams rowParams = new TableRow.LayoutParams(0, android.widget.TableRow.LayoutParams.MATCH_PARENT, 3f);


   TableLayout tableLayout = (TableLayout) findViewById(R.id.thetablelayout);
   tableLayout.removeAllViews();

   for (int i = 0; i < NumberOfRows; i++) {


        TableRow tableRow = new TableRow(this);
        tableRow.setLayoutParams(tableParams);

        Button btnOne = new Button(this);
        btnOne.setId(1001 + i);
        Button btnTwo = new Button(this);
        btnTwo.setId(2001 + i);


        btnOne.setLayoutParams(rowParams);
        btnTwo.setLayoutParams(rowParams);

        tableRow.addView(btnOne);

        View adivider = new View(this);
        adivider.setLayoutParams(new TableRow.LayoutParams(20, TableRow.LayoutParams.MATCH_PARENT));
        adivider.setBackgroundColor(Color.TRANSPARENT);


   // This bit of code deals with odd/even numbers of buttons. 

        if (((i + 1) * 2) < NumberOfButtons + 1) {
            tableRow.addView(adivider);
            tableRow.addView(btnTwo);
            btnTwo.setOnClickListener(mGlobal_OnClickListener);
            btnTwo.setOnLongClickListener(mGlobal_OnLONGClickListener);
            btnTwo.setTextSize(14);
            btnTwo.setSingleLine(true);
            btnTwo.setTextColor(0xFFFFFFFF);
            btnTwo.setText(btnnamearray.get((i * 2) + 1));
            btnTwo.setBackgroundResource(R.drawable.buttonright);
        } else {
            tableRow.addView(adivider);
            btnTwo.setBackgroundResource(android.R.color.transparent);
            tableRow.addView(btnTwo);
        }


        View aline3 = new View(this);
        aline3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 20));
        aline3.setBackgroundColor(Color.TRANSPARENT);


        tableLayout.addView(tableRow);

        tableLayout.addView(aline3);
   }

1 个答案:

答案 0 :(得分:2)

正如您所提到的,我建议将按钮重做为相对布局,这样您就可以在每个按钮视图中嵌套ProgressBar(设置为visibility =“gone”)。如果您发布代码,我会很乐意为您提供帮助。

编辑:好的,我嘲笑你了。花点时间阅读所有内容,尤其是评论!

结果:onClick显示ProgressBar,onLongClick隐藏它

    private void setupTableLayout(int NumberOfRows, int NumberOfButtons){
        TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);
        TableRow.LayoutParams rowParams = new TableRow.LayoutParams(0, android.widget.TableRow.LayoutParams.MATCH_PARENT, 3f);


        TableLayout tableLayout = (TableLayout) findViewById(R.id.thetablelayout);
        tableLayout.removeAllViews();

        for (int i = 0; i < NumberOfRows; i++) {
            TableRow tableRow = new TableRow(this);
            tableRow.setLayoutParams(tableParams);

            RelativeLayout btnOneLayout = (RelativeLayout)getLayoutInflater().inflate(R.layout.custom_button, null);
            RelativeLayout btnTwoLayout = (RelativeLayout)getLayoutInflater().inflate(R.layout.custom_button, null);

            ProgressBar btnOneProgressBar = (ProgressBar)btnOneLayout.findViewById(R.id.progressBar);
            ProgressBar btnTwoProgressBar = (ProgressBar)btnTwoLayout.findViewById(R.id.progressBar);

            Button btnOne = (Button)btnOneLayout.findViewById(R.id.button);
            btnOne.setText("Btn 1, Row " + i);
            btnOne.setId(1001 + i);
            Button btnTwo = (Button)btnTwoLayout.findViewById(R.id.button);
            btnTwo.setText("Btn 2, Row " + i);
            btnTwo.setId(2001 + i);

/*            btnOne.setLayoutParams(rowParams);   //Weight for width set in Layout file on RelativeLayout
            btnTwo.setLayoutParams(rowParams);*/

            setButtonClickListener(btnOneLayout, btnOneProgressBar);
            setButtonLongClickListener(btnOneLayout, btnOneProgressBar);

            tableRow.addView(btnOneLayout); //Add layout, instead of just Button

            //Is this done as an invisible separator? You can instead just use Margin on above/below views
            View adivider = new View(this);
            adivider.setLayoutParams(new TableRow.LayoutParams(20, TableRow.LayoutParams.MATCH_PARENT));
            adivider.setBackgroundColor(Color.TRANSPARENT);

            // This bit of code deals with odd/even numbers of buttons.
            if (((i + 1) * 2) < NumberOfButtons + 1) {
                tableRow.addView(adivider);
                tableRow.addView(btnTwoLayout);

                //Click listeners
                setButtonClickListener(btnTwoLayout, btnTwoProgressBar);
                setButtonLongClickListener(btnTwoLayout, btnTwoProgressBar);

/*                btnTwo.setTextSize(14);              //If true for all buttons, can be done in Layout file
                btnTwo.setSingleLine(true);
                btnTwo.setTextColor(0xFFFFFFFF);*/

                //btnTwo.setText(getButtonName((i * 2) + 1));
                //btnTwo.setBackgroundResource(R.drawable.buttonright); //I didn't have this drawable
            } else {
                tableRow.addView(adivider);

                btnTwoLayout.setBackgroundResource(android.R.color.transparent); //I changed this to overall layout, not sure if this is what you want
                //btnTwoLayout.setVisibility(View.INVISIBLE); //Or View.GONE   //Perhaps you want this instead

                tableRow.addView(btnTwoLayout);
            }

            //Is this done as an invisible separator? You can instead just use Margin/Padding on above/below views
            View aline3 = new View(this);
            aline3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 20));
            aline3.setBackgroundColor(Color.TRANSPARENT);

            tableLayout.addView(tableRow);
            tableLayout.addView(aline3);
        }

    }

    private String getButtonName(int position){
        String name = "";
        switch (position){
            case 0:
                name = "Bob";
                break;
            case 1:
                name = "Jim";
                break;
            default:
                name = "Default";
                break;
        }
        return name;
    }

    private void setButtonClickListener(RelativeLayout layout, final ProgressBar progressBar){
        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Do something
                progressBar.setVisibility(View.VISIBLE);
            }
        });
    }
    private void setButtonLongClickListener(RelativeLayout layout, final ProgressBar progressBar){
        layout.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                //Do something
                progressBar.setVisibility(View.GONE);
                return true;
            }
        });
    }

按照您的意愿调用该功能......

setupTableLayout(10, 2);

必要的布局文件

activity_main.xml中

<RelativeLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <TableLayout
        android:id="@+id/thetablelayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        </TableLayout>
    </ScrollView>
</RelativeLayout>

custom_button.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:background="@android:color/holo_blue_bright"
    android:padding="24dp">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_red_dark"
        android:text="Button"
        android:gravity="center"
        android:textColor="@android:color/white"
        android:singleLine="true"
        android:clickable="false">
    </Button>

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_green_dark"
        android:layout_centerInParent="true"
        android:visibility="gone"
        />
</RelativeLayout>
祝你好运,不要害怕提出更多问题。 : - )