原生Android:动态创建一个网格状的滚动布局,具有相同宽度的按钮
我正试图创建一个布局根据这张图片(画画) 目标水平为API 17。这必须以编程方式创建,而不是使用XML。这必须是响应性设计 我在stackoverflow上广泛研究和尝试了其他部分类似的情况,使用GridLayout、TableLayout、GridView、各种布局参数、重力、重量、视图嵌套等。但是, (a) 我无法根据设备屏幕大小和方向获取文本按钮宽度以填充可用宽度。文本较短的按钮宽度也较短 (b) 无论屏幕大小和方向如何,加号、减号和数字按钮的高度和宽度都是固定的。但它们没有与同一行左侧的文本按钮对齐。只有底部的几个像素可见 我将非常感谢任何能够实现上述布局的代码片段。非常感谢 更新: 根据@tiny sunlight的输入,我做了这个。接下来,我将以编程方式重新创建它原生Android:动态创建一个网格状的滚动布局,具有相同宽度的按钮,android,android-layout,Android,Android Layout,我正试图创建一个布局根据这张图片(画画) 目标水平为API 17。这必须以编程方式创建,而不是使用XML。这必须是响应性设计 我在stackoverflow上广泛研究和尝试了其他部分类似的情况,使用GridLayout、TableLayout、GridView、各种布局参数、重力、重量、视图嵌套等。但是, (a) 我无法根据设备屏幕大小和方向获取文本按钮宽度以填充可用宽度。文本较短的按钮宽度也较短 (b) 无论屏幕大小和方向如何,加号、减号和数字按钮的高度和宽度都是固定的。但它们没有与同一行左
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/layoutTable"
android:padding="5dp"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/layoutRow"
android:padding="5dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_marginEnd="10dp"
android:layout_weight="17"
android:text="This is my button"
android:textSize="15sp"
android:textAllCaps="false"
android:gravity="start"
android:layout_width="0dp"
android:layout_height="40dp" />
<Button
android:layout_marginEnd="5dp"
android:layout_weight="1"
android:text="-"
android:textSize="15sp"
android:textStyle="bold"
android:layout_width="40dp"
android:layout_height="40dp" />
<TextView
android:layout_marginEnd="5dp"
android:layout_weight="1"
android:layout_marginTop="0dp"
android:paddingTop="10dp"
android:text="0"
android:textSize="15sp"
android:gravity="center"
android:layout_width="40dp"
android:layout_height="40dp" />
<Button
android:layout_weight="1"
android:text="+"
android:textSize="15sp"
android:textStyle="bold"
android:layout_width="40dp"
android:layout_height="40dp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:padding="5dp"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:padding="5dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_marginRight="10dp"
android:layout_weight="1"
android:text="1111"
android:layout_width="0dp"
android:layout_height="40dp" />
<ImageView
android:layout_marginRight="5dp"
android:src="@mipmap/ic_launcher"
android:layout_width="40dp"
android:layout_height="40dp" />
<TextView
android:layout_marginRight="5dp"
android:background="#44bb11"
android:layout_marginTop="0dp"
android:paddingTop="10dp"
android:text="11"
android:layout_width="40dp"
android:layout_height="40dp" />
<ImageView
android:src="@mipmap/ic_launcher"
android:layout_width="40dp"
android:layout_height="40dp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
您可以使用回收器视图,它将为您提供滚动选项和动态绑定视图 因此,将线性布局(水平方向)加载到回收器视图中。现在,对于布局,它将变得更容易。你需要一个文本视图,按钮,文本视图,按钮。 对于这些文本视图和按钮中的每一个,分别将widht=0dp和weight设置为7,1,1,1,即最左边的文本视图占宽度的70%,而所有其他文本视图占宽度的10%。显然,您可以根据您的要求更改这些重量。布局就是这样
现在只需使用适配器填充数据。尝试以编程方式构建下面的布局
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/layoutTable"
android:padding="5dp"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/layoutRow"
android:padding="5dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_marginEnd="10dp"
android:layout_weight="17"
android:text="This is my button"
android:textSize="15sp"
android:textAllCaps="false"
android:gravity="start"
android:layout_width="0dp"
android:layout_height="40dp" />
<Button
android:layout_marginEnd="5dp"
android:layout_weight="1"
android:text="-"
android:textSize="15sp"
android:textStyle="bold"
android:layout_width="40dp"
android:layout_height="40dp" />
<TextView
android:layout_marginEnd="5dp"
android:layout_weight="1"
android:layout_marginTop="0dp"
android:paddingTop="10dp"
android:text="0"
android:textSize="15sp"
android:gravity="center"
android:layout_width="40dp"
android:layout_height="40dp" />
<Button
android:layout_weight="1"
android:text="+"
android:textSize="15sp"
android:textStyle="bold"
android:layout_width="40dp"
android:layout_height="40dp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:padding="5dp"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:padding="5dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_marginRight="10dp"
android:layout_weight="1"
android:text="1111"
android:layout_width="0dp"
android:layout_height="40dp" />
<ImageView
android:layout_marginRight="5dp"
android:src="@mipmap/ic_launcher"
android:layout_width="40dp"
android:layout_height="40dp" />
<TextView
android:layout_marginRight="5dp"
android:background="#44bb11"
android:layout_marginTop="0dp"
android:paddingTop="10dp"
android:text="11"
android:layout_width="40dp"
android:layout_height="40dp" />
<ImageView
android:src="@mipmap/ic_launcher"
android:layout_width="40dp"
android:layout_height="40dp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
根据您的提示,我这样做是为了测试。接下来,我将尝试以编程方式重新创建它。我认为使用xml与以编程方式创建相同。我将只在运行时知道行数和行内容。所以xml没有帮助,对吗?我喜欢下面@rusheel jain的RecyclerView创意。也许我会在下一次迭代中使用它。谢谢你们的及时回复。是的。RecyclerView是更好的选择,而RecyclerView的项目是xml。