views 和 margin 之间的间距太大并不能解决问题
A lot of spacing between views and margin doesn't do the stuff
我有一个带有垂直 LinearLayout 的 HorizontalScrollView。我在那里添加了一些相同类型的自定义视图。默认情况下,视图之间有很多间距。所以我想我必须将我的观点的边距设置为 0 或其他东西。但是完全没有结果。
首先,我尝试更改 xml
中的边距
<gui.CardUi
android:id="@+id/cardUi"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="0dp"
</gui.CardUi>
比起我尝试更改代码中的边距:
private void setMargins ( int left, int top, int right, int bottom) {
if (getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) getLayoutParams();
p.setMargins(left, top, right, bottom);
requestLayout();
}
}
setMargins(0, 0, 0, 0);
不确定信息是否重要,但我使用 LayoutInflater 以编程方式添加视图。
由于您夸大视图的方式,您的边距不会被设置。
我猜你用过这样的方式:
View child = getLayoutInflater().inflate(R.layout.mylayout, null);
关键是当你想要膨胀一个视图并将其添加到另一个视图时,你应该通知膨胀器关于容器视图(在你的示例线性布局中)。因此,您的布局参数(如边距、重量和重力)将被正确设置。
所以改用这个方法:
View child = getLayoutInflater().inflate(R.layout.child, item, false);
您的代码中无需添加边距。
我有一个带有垂直 LinearLayout 的 HorizontalScrollView。我在那里添加了一些相同类型的自定义视图。默认情况下,视图之间有很多间距。所以我想我必须将我的观点的边距设置为 0 或其他东西。但是完全没有结果。
首先,我尝试更改 xml
<gui.CardUi
android:id="@+id/cardUi"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="0dp"
</gui.CardUi>
比起我尝试更改代码中的边距:
private void setMargins ( int left, int top, int right, int bottom) {
if (getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) getLayoutParams();
p.setMargins(left, top, right, bottom);
requestLayout();
}
}
setMargins(0, 0, 0, 0);
不确定信息是否重要,但我使用 LayoutInflater 以编程方式添加视图。
由于您夸大视图的方式,您的边距不会被设置。 我猜你用过这样的方式:
View child = getLayoutInflater().inflate(R.layout.mylayout, null);
关键是当你想要膨胀一个视图并将其添加到另一个视图时,你应该通知膨胀器关于容器视图(在你的示例线性布局中)。因此,您的布局参数(如边距、重量和重力)将被正确设置。 所以改用这个方法:
View child = getLayoutInflater().inflate(R.layout.child, item, false);
您的代码中无需添加边距。