java - 如何相对于表格布局中的另一个 View 添加一个 View (例如圆形进度条)

标签 java android android-studio

希望有人指教。

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

我想在每个按钮前添加另一个 View (以圆形进度条的形式),以便在按下按钮时圆圈旋转,同时按钮已设置为执行任何操作。我希望进度条最好位于按钮的中间,但我并不过分挑剔。

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

谢谢

我当前的按钮代码...

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

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

最佳答案

正如您提到的,我建议将按钮重做为相对布局,这样您就可以在每个按钮 View 中嵌套 ProgressBar(set to 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>

自定义按钮.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>

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

关于java - 如何相对于表格布局中的另一个 View 添加一个 View (例如圆形进度条),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31303607/

相关文章:

java - SPARQL 类型转换?

JAVA Apache HttpClient SSL 超时

Android 客户端终止连接

java - 是否真的有一种方法可以保存访问过的网站的历史记录,以便当用户返回(重新打开应用程序)时,他可以在后按时获得访问过的网站列表

java - 通过GSON转换后如何获取对象的字段key?

java - 将 matplotlib (Python) 与 Android Studio 集成

android - 在真实设备上进行测试时,uses-library com.android.nfc_extras 会破坏 API 26 及更高版本的 AndroidTest

java.net.SocketException : Socket is closed

java - 如何从类中隐藏不支持的接口(interface)方法

java - Android Studio 传递变量