【问题标题】:how to remove space between views in android studio [duplicate]如何在android studio中删除视图之间的空间[重复]
【发布时间】:2017-01-15 15:18:44
【问题描述】:

我想删除TextViewButton 之间的空格 我尝试了很多选择,但现在没有人工作。 提前感谢,任何帮助将不胜感激。 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 view space android-layout-weight


【解决方案1】:

为按钮添加背景颜色并相应地更改文本颜色。这样空间就会被删除。

<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"/>

任何自定义颜色都可以。

【讨论】:

    【解决方案2】:

    您总是可以将 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>
    

    然后在您的活动中:

    TextView button = (TextView) findViewById(R.id.tv_button);
    button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Do some stuff here
            }
        });
    

    【讨论】:

      【解决方案3】:

      使用负边距。

      android:margin=-2dp

      编辑:如果你想避免负边距,你必须创建自己的(按钮)视图,因为标准按钮视图带有该边距,所以你不能删除它

      【讨论】:

      • 首先,感谢@Aenadon second 的回答。我读到我们应该避免负值。它没有工作。
      • 好的,请阅读我的更新答案
      • 哦,你的意思是在颜色上弄个花样,这样空间就不会被看到了? ?
      • 不,我的意思是你使用的东西会填满整个空间,例如,就拿TextView,你不会有按钮动画但它会填满空间
      • 嘿,如果我的回答对你有帮助,请将我的回答标记为正确...如果没有,请告诉我出了什么问题
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-16
      • 2011-11-30
      • 2021-08-13
      • 1970-01-01
      相关资源
      最近更新 更多