Java 如何相对于tablelayout中的另一个视图添加视图(例如圆形进度条)
希望有人能提出建议 我在androidstudio中创建了一个Android应用程序,可以通过编程动态地将大量按钮添加到tablelayout中。按钮的数量由用户设置,因此可能有1个或100个。使用tablelayout,每行整齐地添加两个按钮,并且大小正确。我对此感到高兴 我想在每个按钮前面添加另一个视图(以圆形进度条的形式),这样当按下按钮时,圆圈将旋转,而按钮设置要执行的操作将被执行。我希望进度栏最好坐在按钮中间,但我不是过于挑剔。 我能得到一些关于实现这一目标的最佳方法的建议吗?我需要在相对布局中重做按钮吗?是否有某种方法可以在表布局中的其他视图之上添加视图?欢迎任何意见或建议 多谢各位 我当前的按钮代码 (假设每行有两个按钮,您知道行数和按钮数)Java 如何相对于tablelayout中的另一个视图添加视图(例如圆形进度条),java,android,android-studio,Java,Android,Android Studio,希望有人能提出建议 我在androidstudio中创建了一个Android应用程序,可以通过编程动态地将大量按钮添加到tablelayout中。按钮的数量由用户设置,因此可能有1个或100个。使用tablelayout,每行整齐地添加两个按钮,并且大小正确。我对此感到高兴 我想在每个按钮前面添加另一个视图(以圆形进度条的形式),这样当按下按钮时,圆圈将旋转,而按钮设置要执行的操作将被执行。我希望进度栏最好坐在按钮中间,但我不是过于挑剔。 我能得到一些关于实现这一目标的最佳方法的建议吗?我需要在
TableLayout.LayoutParams tableParams=新建TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_内容,TableLayout.LayoutParams.WRAP_内容);
TableRow.LayoutParams rowParams=新建TableRow.LayoutParams(0,android.widget.TableRow.LayoutParams.MATCH_父级,3f);
TableLayout TableLayout=(TableLayout)findViewById(R.id.thetablelayout);
tableLayout.removeallview();
for(int i=0;i
正如您所提到的,我建议将按钮作为相对布局重做,这将允许您在每个按钮视图中嵌套一个ProgressBar(设置为visibility=“gone”)。如果你发布你的代码,我很乐意进一步帮助你
编辑:好吧,我给你做了个模型。花点时间阅读所有内容,尤其是评论
结果:onClick显示进度条,onLongClick隐藏进度条
以及必要的布局文件
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>
祝你好运,不要害怕问更多的问题。:-) 谢谢你的建议。我已经拥有的创建按钮的代码现在包含在原始帖子中。如果你能建议我如何在一个相对的布局中获得同样的结果,我会非常感激。@Regoulous不确定它是否会向你发送编辑通知,所以我也会这样做。哇-这绝对令人惊讶,正是我所需要的。非常感谢你抽出时间给我看。今晚我要试试这个。再次非常感谢。你是个绅士。再次感谢你的帮助,但我想知道我是否可以再加一点。我已经模拟了这个,就像你给我看的一样,但是我不能让进度条出现。我甚至尝试将可见性设置为在XML文件中可见,但没有显示进度条。我知道onclick事件正在工作,因为我添加了一个toast,只是为了弹出一条消息来证明它,但是当我运行上面的代码时,我得到的只是按钮。谢谢你,好吧,我想了一部分。如果我使按钮背景透明,我就可以看到进度条。奇怪的我会先做些修补工作。非常感谢。
setupTableLayout(10, 2);
<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 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>