我想在项目中使用负值作为保证金。我发现它符合我的目的(0dp值余量仍然太多)。但是一般标签" layout_margin"由于某种原因不起作用。
这是带有原始示例的main.xml,用于说明问题:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/main"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent">
<Button
android:layout_margin="@dimen/default_button_margin"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="New Button"
android:id="@+id/button1"/>
<Button
android:layout_marginLeft="@dimen/default_button_margin"
android:layout_marginRight="@dimen/default_button_margin"
android:layout_marginBottom="@dimen/default_button_margin"
android:layout_marginTop="@dimen/default_button_margin"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="New Button"
android:id="@+id/button2"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent">
<Button
android:layout_marginLeft="@dimen/default_button_margin"
android:layout_marginRight="@dimen/default_button_margin"
android:layout_marginBottom="@dimen/default_button_margin"
android:layout_marginTop="@dimen/default_button_margin"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="New Button"
android:id="@+id/button3"/>
<Button
android:layout_marginLeft="@dimen/default_button_margin"
android:layout_marginRight="@dimen/default_button_margin"
android:layout_marginBottom="@dimen/default_button_margin"
android:layout_marginTop="@dimen/default_button_margin"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="New Button"
android:id="@+id/button4"/>
</LinearLayout>
</LinearLayout>
没有导入语句的MainActivity.java:
public class MainActivity extends Activity {
private final int FIELD_SIZE = 3;
//private LinearLayout mainLayout = new LinearLayout(this);
private Collection<LinearLayout> layouts = new LinkedList<>();
private final Button[][] buttons = new Button[FIELD_SIZE][FIELD_SIZE];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
和dimens.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="default_button_margin">-3dp</dimen>
</resources>
不幸的是,由于声誉,我没有可能发布图片。