【问题标题】:How to add a view (circular progress bar for example) relative to another view in a tablelayout如何在表格布局中相对于另一个视图添加视图(例如圆形进度条)
【发布时间】:2015-07-08 20:58:50
【问题描述】:

希望有人可以提供建议。

我在 Android Studio 中创建了一个 Android 应用程序,它以动态和编程方式将大量按钮添加到表格布局中。按钮的数量由用户设置,因此可能有 1 个或可能有 100 个。使用表格布局,它们可以整齐地添加两个按钮到每行并且大小正确。我对此很满意。

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

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

谢谢

我当前的按钮代码...

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

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

【问题讨论】:

    标签: java android android-studio


    【解决方案1】:

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

    编辑:好的,我为你模拟了这个。花点时间阅读所有内容,尤其是 cmets!

    结果: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>
    

    祝你好运,不要害怕提出更多问题。 :-)

    【讨论】:

    • 感谢您的建议。我已经拥有的创建按钮的代码现在包含在原始帖子中。如果您能建议我如何在相对布局中实现相同的结果,那将不胜感激。
    • @Regnodulous 不确定它是否会向您发送编辑通知,所以我也会这样做。
    • 哇——这真是太棒了,正是我所需要的。非常感谢您花时间向我展示。我今晚试试这个。再次感谢。你是个绅士。
    • 再次感谢您的帮助,但我想知道是否可以多加一点。我已经完全按照您向我展示的那样模拟了这一点,但我一生都无法让进度条出现。我什至尝试在 XML 文件中将可见性设置为可见,但没有显示进度条。我知道 onclick 事件正在工作,因为我添加了一个 toast 只是为了弹出一条消息来证明它,但是当我运行上面的代码时我得到的只是按钮。谢谢
    • 好的 - 想出了一部分。如果我使按钮背景透明,我可以看到进度条。奇怪的。会先做更多的修补。谢谢
    猜你喜欢
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-14
    • 1970-01-01
    • 2020-06-21
    • 2021-12-10
    相关资源
    最近更新 更多