【发布时间】:2016-11-13 07:30:06
【问题描述】:
我有一个表格,每行包含 3 个按钮。 现在,我想在一行中的每个按钮之间添加一些填充.. 我该怎么做..? 当我在代码中添加此语句时,
tableRow.setPadding(20,20,20,20);
我能够观察到每一行之间的填充。 我想在每个按钮之间有填充...
注意:我想从 java 编程,而不是从 xml..
【问题讨论】:
-
在按钮上设置填充
我有一个表格,每行包含 3 个按钮。 现在,我想在一行中的每个按钮之间添加一些填充.. 我该怎么做..? 当我在代码中添加此语句时,
tableRow.setPadding(20,20,20,20);
我能够观察到每一行之间的填充。 我想在每个按钮之间有填充...
注意:我想从 java 编程,而不是从 xml..
【问题讨论】:
你可以这样做:
<Button android:id="@+id/myBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="Click Me"
/>
【讨论】:
padding 是按钮内边缘和文本之间的边距。
这将增加按钮大小而不是按钮之间的距离
您需要在按钮标签内的 XML 中使用这样的边距
android:layout_margin="10dp"
这会将所有边距设置为10dp
如果您只需要为某些侧面使用设置边距:
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
我希望这对您有所帮助。
问候
【讨论】:
您需要为按钮添加内边距。
btn1.setPadding(left, top, right, bottom);
btn2.setPadding(left, top, right, bottom);
.
.
.
还要确保以 dp 为单位,硬编码的值会导致在不同分辨率的设备上填充不均匀。
【讨论】:
TableRow 类和所有 View 子类一样,确实有一个 setPadding 方法。
但是,既然您提到您找到了 setMargin,我相信您正在查看 TableRow.LayoutParams 而不是 TableRow 本身。
边距在视图的 LayoutParams 中设置,而内边距在视图中设置。
【讨论】:
您需要为按钮使用边距,而不是为整行填充。因为填充实际上是按钮文本和边框之间的空间,例如内部间距,而边距是按钮边框和它的容器(行)之间的空间,例如外太空
根据您想要达到的外观,使用类似的东西
<Button android:id="@+id/yourButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="Button1"
/>
<Button android:id="@+id/yourButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="Button2"
/>
<Button android:id="@+id/yourButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="Button3"
/>
【讨论】: