【问题标题】:remove space or change color between the views in android在android中的视图之间删除空间或更改颜色
【发布时间】:2017-05-29 19:12:59
【问题描述】:

如下图所示,按钮和文本视图之间存在间隙。我需要删除这个空间,或者我需要将其颜色更改为原色。这是我的代码和快照

enter image description here

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:weightSum="5">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_weight="2"
            android:orientation="vertical"
            android:background="@color/colorPrimary"
            android:paddingRight="2dp"
            android:paddingLeft="2dp"
            android:layout_height="170dp">

            <android.support.v4.view.ViewPager
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/splash"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                />
        </LinearLayout>

        <LinearLayout

                android:id="@+id/logo1"
                android:layout_width="fill_parent"
                android:layout_height="100sp"
                android:orientation="horizontal"
                android:layout_weight="1"
                android:padding="2dp"
                android:background="@color/colorPrimary">

                <LinearLayout
                    android:id="@+id/mobileRelView"
                    android:orientation="vertical"
                    android:weightSum="2"
                    android:clickable="true"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingRight="1dp"
                    android:onClick="goToMobileForm"
                    android:layout_weight="0.25">

                    <ImageView
                        android:padding="7dp"
                        android:id="@+id/mobile_Image"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:elevation="1dp"
                        android:clickable="false"
                        android:layout_weight="0.7"
                        android:scaleType="fitCenter"
                        android:src="@drawable/smartphone"/>


                    <Button
                        android:id="@+id/mobile_button"
                        android:layout_width="match_parent"
                        android:layout_weight="1.3"
                        android:clickable="false"
                        android:layout_height="match_parent"
                        android:background="@color/colorPrimary"
                        android:text="Mobile"
                        android:textAllCaps="false"
                        android:textColor="#000000"
                        android:textSize="@dimen/mainbuttontext"/>
                    <View
                        android:layout_width="70dp"
                        android:layout_height="2dp"
                        android:layout_alignParentRight="true"
                        android:layout_gravity="center"
                        android:background="#424242" />
                </LinearLayout>
                <View
                    android:layout_width="2dp"
                    android:layout_height="50dp"
                    android:layout_gravity="center"
                    android:background="#424242" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:onClick="goToDesktopForm"
                    android:weightSum="2"
                    android:clickable="true"
                    android:paddingLeft="2dp"
                    android:paddingRight="1dp"
                    android:layout_weight="0.25">

                    <ImageView
                        android:id="@+id/desktop_Image"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="0.7"
                        android:clickable="false"
                        android:scaleType="fitCenter"
                        android:src="@drawable/compuericon"/>

                    <Button
                        android:text="Desktop"
                        android:textAllCaps="false"
                        android:layout_width="match_parent"
                        android:clickable="false"
                        android:layout_height="match_parent"
                        android:id="@+id/desktop_button"
                        android:background="@color/colorPrimary"
                        android:textSize="@dimen/mainbuttontext"
                        android:textColor="#000000"
                        android:layout_weight="1.3" />
                    <View
                        android:layout_width="70dp"
                        android:layout_height="2dp"
                        android:layout_alignParentRight="true"
                        android:layout_gravity="center"
                        android:background="#424242" />
                </LinearLayout>

                <View
                    android:layout_width="2dp"
                    android:layout_height="50dp"
                    android:layout_gravity="center"
                    android:background="#424242" />
                <LinearLayout
                    android:layout_width="match_parent"
                    android:paddingLeft="1dp"
                    android:orientation="vertical"
                    android:onClick="goToLaptopForm"
                    android:weightSum="2"
                    android:layout_height="match_parent"
                    android:layout_weight="0.25">

                    <ImageView
                        android:id="@+id/laptop_Image"
                        android:layout_width="match_parent"
                        android:layout_weight="0.7"
                        android:clickable="false"
                        android:layout_height="match_parent"
                        android:scaleType="fitCenter"
                        android:src="@drawable/laptop" />

                    <Button
                        android:text="Laptop"
                        android:textAllCaps="false"
                        android:clickable="false"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/laptop_button"
                        android:textSize="@dimen/mainbuttontext"
                        android:background="@color/colorPrimary"
                        android:textColorHint="#ffffff"
                        android:layout_weight="1.3"
                        android:textColor="#000000"/>
                    <View
                        android:layout_width="70dp"
                        android:layout_height="2dp"
                        android:layout_alignParentRight="true"
                        android:layout_gravity="center"
                        android:background="#424242" />

                </LinearLayout>

            </LinearLayout>


            <LinearLayout
                android:id="@+id/logobutton1"
                android:layout_width="fill_parent"
                android:layout_height="100sp"
                android:orientation="horizontal"
                android:layout_weight="1"
                android:padding="2dp"
                android:background="@color/colorPrimary">

                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingRight="2dp"
                    android:paddingLeft="1dp"
                    android:onClick="gotToOthersForm"
                    android:weightSum="2"
                    android:layout_weight="0.25">

                    <ImageView
                        android:id="@+id/homeappliances_Image"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="0.7"
                        android:padding="7dp"
                        android:clickable="false"
                        android:scaleType="fitCenter"
                        android:src="@drawable/washingmachine"/>

                    <Button
                        android:text="Home Appliance"
                        android:textAllCaps="false"
                        android:clickable="false"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/homeappliances_button"
                        android:background="@color/colorPrimary"
                        android:textSize="@dimen/mainbuttontext"
                        android:layout_weight="1.3"
                        android:textColor="#000000"/>

                </LinearLayout>
                <View
                    android:layout_width="2dp"
                    android:layout_height="50dp"
                    android:layout_gravity="center"
                    android:background="#424242" />
                <LinearLayout
                    android:orientation="vertical"                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingRight="1dp"
                    android:weightSum="2"
                    android:onClick="goToDesktopForm"
                    android:layout_weight="0.25">

                    <ImageView
                        android:layout_weight="0.7"
                        android:clickable="false"
                        android:id="@+id/cleaning_Image"
                        android:padding="7dp"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:scaleType="fitCenter"
                        android:src="@drawable/caricon" />

                    <Button
                        android:text="Automobile"
                        android:textAllCaps="false"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/cleaning_button"
                        android:background="@color/colorPrimary"
                        android:textSize="@dimen/mainbuttontext"
                        android:layout_weight="1.3"
                        android:clickable="false"
                        android:textColor="#000000" />

                </LinearLayout>
                <View
                    android:layout_width="2dp"
                    android:layout_height="50dp"
                    android:layout_gravity="center"
                    android:background="#424242" />
                <LinearLayout
                    android:orientation="vertical"
                    android:weightSum="2"
                    android:onClick="gotToOthersForm"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingLeft="1dp"
                    android:layout_weight="0.25">

                    <ImageView
                        android:layout_weight="0.7"
                        android:id="@+id/othrs_Image"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:scaleType="fitCenter"
                        android:padding="7dp"
                        android:clickable="false"
                        android:src="@drawable/othersicon" />

                    <Button
                        android:text="Others"
                        android:textAllCaps="false"
                        android:clickable="false"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/others_button"
                        android:background="@color/colorPrimary"
                        android:textSize="@dimen/mainbuttontext"
                        android:layout_weight="1.3"
                        android:textColor="#000000"
                        android:layout_alignParentTop="true"
                        android:layout_centerHorizontal="true" />

                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:orientation="vertical"
                android:layout_height="150sp">

                <!--<TextView
                    android:id="@+id/textView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="#6A1B9A"
                    android:textStyle="bold"
                    android:background="#ffb300"
                    android:textSize="15sp"
                    android:text="Why eFix ?" />
                    -->

                <ImageView
                    android:id="@+id/imageView1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingTop="2dp"
                    android:scaleType="centerCrop"
                    app:srcCompat="@drawable/abca" />

                <ImageView
                    android:id="@+id/imageView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </LinearLayout>

        </LinearLayout>

【问题讨论】:

    标签: java android xml android-layout


    【解决方案1】:

    这是由于使用了Button 而发生的。使用TextView 而不是Button

    #. 我已经修改了您的 XML 并删除了一些多余的属性,并使用正确的 weightheight 更新了 ImageViewTextView

    #. 我已将属性android:background="?attr/selectableItemBackground" 添加到LinearLayout 以在点击时显示ripple 效果。

    这是工作代码:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:weightSum="5">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_weight="2"
                android:orientation="vertical"
                android:background="@color/colorPrimary"
                android:paddingRight="2dp"
                android:paddingLeft="2dp"
                android:layout_height="170dp">
    
                <android.support.v4.view.ViewPager
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:app="http://schemas.android.com/apk/res-auto"
                    android:id="@+id/splash"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" />
            </LinearLayout>
    
            <LinearLayout
                android:id="@+id/logo1"
                android:layout_width="fill_parent"
                android:layout_height="100sp"
                android:orientation="horizontal"
                android:layout_weight="1"
                android:padding="2dp"
                android:background="@color/colorPrimary">
    
                <LinearLayout
                    android:id="@+id/mobileRelView"
                    android:orientation="vertical"
                    android:weightSum="2"
                    android:clickable="true"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingRight="1dp"
                    android:onClick="goToMobileForm"
                    android:layout_weight="0.25"
                    android:background="?attr/selectableItemBackground">
    
                    <ImageView
                        android:padding="7dp"
                        android:id="@+id/mobile_Image"
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:elevation="1dp"
                        android:clickable="false"
                        android:layout_weight="1.3"
                        android:scaleType="fitCenter"
                        android:src="@mipmap/ic_launcher"/>
    
                    <TextView
                        android:id="@+id/mobile_button"
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="0.7"
                        android:gravity="center"
                        android:clickable="false"
                        android:textAllCaps="false"
                        android:textColor="#000000"
                        android:textSize="@dimen/mainbuttontext"
                        android:text="Mobile" />
    
                    <View
                        android:layout_width="70dp"
                        android:layout_height="2dp"
                        android:layout_alignParentRight="true"
                        android:layout_gravity="center"
                        android:background="#424242" />
                </LinearLayout>
    
                <View
                    android:layout_width="2dp"
                    android:layout_height="50dp"
                    android:layout_gravity="center"
                    android:background="#424242" />
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:onClick="goToDesktopForm"
                    android:weightSum="2"
                    android:clickable="true"
                    android:paddingLeft="2dp"
                    android:paddingRight="1dp"
                    android:layout_weight="0.25"
                    android:background="?attr/selectableItemBackground">
    
                    <ImageView
                        android:padding="7dp"
                        android:id="@+id/desktop_Image"
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="1.3"
                        android:clickable="false"
                        android:scaleType="fitCenter"
                        android:src="@mipmap/ic_launcher"/>
    
                    <TextView
                        android:id="@+id/desktop_button"
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="0.7"
                        android:gravity="center"
                        android:clickable="false"
                        android:textAllCaps="false"
                        android:textColor="#000000"
                        android:textSize="@dimen/mainbuttontext"
                        android:text="Desktop" />
    
                    <View
                        android:layout_width="70dp"
                        android:layout_height="2dp"
                        android:layout_alignParentRight="true"
                        android:layout_gravity="center"
                        android:background="#424242" />
                </LinearLayout>
    
                <View
                    android:layout_width="2dp"
                    android:layout_height="50dp"
                    android:layout_gravity="center"
                    android:background="#424242" />
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:paddingLeft="1dp"
                    android:orientation="vertical"
                    android:onClick="goToLaptopForm"
                    android:weightSum="2"
                    android:layout_height="match_parent"
                    android:layout_weight="0.25"
                    android:background="?attr/selectableItemBackground">
    
                    <ImageView
                        android:padding="7dp"
                        android:id="@+id/laptop_Image"
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="1.3"
                        android:clickable="false"
                        android:scaleType="fitCenter"
                        android:src="@mipmap/ic_launcher" />
    
                    <TextView
                        android:id="@+id/laptop_button"
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="0.7"
                        android:gravity="center"
                        android:clickable="false"
                        android:textAllCaps="false"
                        android:textColor="#000000"
                        android:textSize="@dimen/mainbuttontext"
                        android:text="Laptop" />
    
                    <View
                        android:layout_width="70dp"
                        android:layout_height="2dp"
                        android:layout_alignParentRight="true"
                        android:layout_gravity="center"
                        android:background="#424242" />
    
                </LinearLayout>
            </LinearLayout>
    
            <LinearLayout
                android:id="@+id/logobutton1"
                android:layout_width="fill_parent"
                android:layout_height="100sp"
                android:orientation="horizontal"
                android:layout_weight="1"
                android:padding="2dp"
                android:background="@color/colorPrimary">
    
                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingRight="2dp"
                    android:paddingLeft="1dp"
                    android:onClick="gotToOthersForm"
                    android:weightSum="2"
                    android:layout_weight="0.25"
                    android:background="?attr/selectableItemBackground">
    
    
                    <ImageView
                        android:id="@+id/homeappliances_Image"
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="1.3"
                        android:padding="7dp"
                        android:clickable="false"
                        android:scaleType="fitCenter"
                        android:src="@mipmap/ic_launcher"/>
    
                    <TextView
                        android:id="@+id/homeappliances_button"
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="0.7"
                        android:gravity="center"
                        android:clickable="false"
                        android:textAllCaps="false"
                        android:textColor="#000000"
                        android:textSize="@dimen/mainbuttontext"
                        android:text="Home Appliance" />
    
                </LinearLayout>
    
                <View
                    android:layout_width="2dp"
                    android:layout_height="50dp"
                    android:layout_gravity="center"
                    android:background="#424242" />
    
                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingRight="1dp"
                    android:weightSum="2"
                    android:onClick="goToDesktopForm"
                    android:layout_weight="0.25"
                    android:background="?attr/selectableItemBackground">
    
                    <ImageView
                        android:id="@+id/cleaning_Image"
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="1.3"
                        android:clickable="false"
                        android:scaleType="fitCenter"
                        android:src="@mipmap/ic_launcher"
                        android:padding="7dp" />
    
                    <TextView
                        android:id="@+id/cleaning_button"
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="0.7"
                        android:gravity="center"
                        android:clickable="false"
                        android:textAllCaps="false"
                        android:textColor="#000000"
                        android:textSize="@dimen/mainbuttontext"
                        android:text="Automobile" />
    
                </LinearLayout>
    
                <View
                    android:layout_width="2dp"
                    android:layout_height="50dp"
                    android:layout_gravity="center"
                    android:background="#424242" />
    
                <LinearLayout
                    android:orientation="vertical"
                    android:weightSum="2"
                    android:onClick="gotToOthersForm"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingLeft="1dp"
                    android:layout_weight="0.25"
                    android:background="?attr/selectableItemBackground">
    
                    <ImageView
                        android:id="@+id/othrs_Image"
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="1.3"
                        android:scaleType="fitCenter"
                        android:clickable="false"
                        android:src="@mipmap/ic_launcher"
                        android:padding="7dp" />
    
                    <TextView
                        android:id="@+id/others_button"
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="0.7"
                        android:gravity="center"
                        android:clickable="false"
                        android:textAllCaps="false"
                        android:textColor="#000000"
                        android:textSize="@dimen/mainbuttontext"
                        android:text="Others"/>
    
                </LinearLayout>
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:orientation="vertical"
                android:layout_height="150sp">
    
                <!--<TextView
                    android:id="@+id/textView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="#6A1B9A"
                    android:textStyle="bold"
                    android:background="#ffb300"
                    android:textSize="15sp"
                    android:text="Why eFix ?" />
                    -->
    
                <ImageView
                    android:id="@+id/imageView1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingTop="2dp"
                    android:scaleType="centerCrop"
                    android:src="@mipmap/ic_launcher" />
    
                <ImageView
                    android:id="@+id/imageView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
    
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
    

    输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-15
      • 1970-01-01
      • 1970-01-01
      • 2020-10-16
      • 2012-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多