如何在 android 工作室中删除视图之间的 space
how to remove space between views in android studio
我想删除 TextView
和 Button
之间的 space
我尝试了很多选择,但现在没有人工作。
提前致谢,我们将不胜感激。
the xml code and preview
这是代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.myapplication.MainActivity">
<TextView
android:layout_margin="0dp"
android:gravity="center"
android:textColor="#000000"
android:textSize="33sp"
android:text="hello"
android:background="#cc3"
android:layout_width = "match_parent"
android:layout_height = "0dp"
android:layout_weight = "3"/>
<Button
android:layout_margin="0dp"
android:layout_width = "match_parent"
android:layout_weight = "1"
android:layout_height = "0dp"
android:text="Click"/>
</LinearLayout>
使用负边距。
android:margin=-2dp
编辑:如果你想避免负边距,你必须创建你自己的(按钮)视图,因为标准按钮视图带有那个边距,所以你不能删除它
您总是可以将 Button 换成另一个 TextView 并向其添加 OnClickListener 吗?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.myapplication.MainActivity">
<TextView
android:gravity="center"
android:textColor="#000000"
android:textSize="33sp"
android:text="hello"
android:background="#cc3"
android:layout_width = "match_parent"
android:layout_height = "0dp"
android:layout_weight = "3"/>
<TextView
android:id="@+id/tv_button"
android:layout_width = "match_parent"
android:layout_weight = "1"
android:layout_height = "0dp"
android:gravity="center"
android:textAllCaps="true"
android:text="Click"/>
</LinearLayout>
然后在你的 Activity:
TextView button = (TextView) findViewById(R.id.tv_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Do some stuff here
}
});
为按钮添加背景颜色并相应地更改文本颜色。这样 space 将被删除。
<Button
android:layout_margin="0dp"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:text="Click"
android:background="@color/common_google_signin_btn_text_dark_focused"
android:layout_below="@+id/a"/>
任何自定义颜色都可以。
我想删除 TextView
和 Button
之间的 space
我尝试了很多选择,但现在没有人工作。
提前致谢,我们将不胜感激。
the xml code and preview
这是代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.myapplication.MainActivity">
<TextView
android:layout_margin="0dp"
android:gravity="center"
android:textColor="#000000"
android:textSize="33sp"
android:text="hello"
android:background="#cc3"
android:layout_width = "match_parent"
android:layout_height = "0dp"
android:layout_weight = "3"/>
<Button
android:layout_margin="0dp"
android:layout_width = "match_parent"
android:layout_weight = "1"
android:layout_height = "0dp"
android:text="Click"/>
</LinearLayout>
使用负边距。
android:margin=-2dp
编辑:如果你想避免负边距,你必须创建你自己的(按钮)视图,因为标准按钮视图带有那个边距,所以你不能删除它
您总是可以将 Button 换成另一个 TextView 并向其添加 OnClickListener 吗?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.myapplication.MainActivity">
<TextView
android:gravity="center"
android:textColor="#000000"
android:textSize="33sp"
android:text="hello"
android:background="#cc3"
android:layout_width = "match_parent"
android:layout_height = "0dp"
android:layout_weight = "3"/>
<TextView
android:id="@+id/tv_button"
android:layout_width = "match_parent"
android:layout_weight = "1"
android:layout_height = "0dp"
android:gravity="center"
android:textAllCaps="true"
android:text="Click"/>
</LinearLayout>
然后在你的 Activity:
TextView button = (TextView) findViewById(R.id.tv_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Do some stuff here
}
});
为按钮添加背景颜色并相应地更改文本颜色。这样 space 将被删除。
<Button
android:layout_margin="0dp"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:text="Click"
android:background="@color/common_google_signin_btn_text_dark_focused"
android:layout_below="@+id/a"/>
任何自定义颜色都可以。