Android Button not filling full space in RelativeLayout

断了今生、忘了曾经 提交于 2019-12-11 11:16:23

问题


I am trying to create a button, aligned to the bottom of the page. I want it to fill 100% space, from left, right and bottom. Whatever attributes I will choose, there are still very small non covered areas. Can you please help?

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    tools:context="com.example.myapplication.MainActivity">

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_margin="0dp"
        android:padding="0dp"
        android:text="search"

        />
</RelativeLayout>

Here is the screenshot, showing not filled areas


回答1:


The space you see is the shadow around the button in its background drawable. You need to create your own background drawable and it should then completely occupy the width.

Or, you could also just set the background to null.

android:background="@null"



回答2:


Create a file in drawable folder like this, say simple_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="false">
        <shape android:shape="rectangle">
            <!-- Normal color -->
            <solid android:color="@android:color/white" />
        </shape>
    </item>

    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <!-- Color - when the view is pressed -->
            <solid android:color="@android:color/black"/>
        </shape>
    </item>
</selector>

Now simply set the background as the above created drawable.

<Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:background="@drawable/simple_selector"
        android:text="search"

        />

The above code can change color in both normal and pressed states of the button. You can change the colors as per your requirement.



来源:https://stackoverflow.com/questions/35186506/android-button-not-filling-full-space-in-relativelayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!