2013-05-03 104 views
0

我是新来的android开发,我想让我的按钮覆盖页面的整个宽度,没有任何一方的空间。我研究了这一点,并使用Android的重量不起作用。如何使用相对布局使按钮填充屏幕(宽度)?

这里是XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    style="@style/AppTheme" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/background" 
    android:icon="@drawable/ic_launcher" 
    android:scrollbars="horizontal|vertical" 
    tools:context=".MainActivity" > 


    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="fill_parent" 
     android:layout_height="98dp" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:background="@drawable/buttonpress" 
     android:text="@string/hello_world" 
     android:textColor="#228496" 
     android:textSize="14sp" /> 

    <Button 
     android:id="@+id/categories" 
     android:layout_width="fill_parent" 
     android:layout_height="50dp" 
     android:layout_below="@+id/textView1" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="14dp" 
     android:background="@drawable/buttonpress" 
     android:onClick="Categories" 
     android:text="@string/title_activity_categories" 
     android:textColor="#228496" 
     android:textSize="21sp" /> 

    <Button 
     android:id="@+id/feedback" 
     android:layout_width="fill_parent" 
     android:layout_height="50dp" 
     android:layout_alignLeft="@+id/categories" 
     android:layout_below="@+id/categories" 
     android:layout_marginTop="15dp" 
     android:background="@drawable/buttonpress" 
     android:onClick="feedback" 
     android:text="@string/Feedback" 
     android:textColor="#228496" 
     android:textSize="21sp" /> 

</RelativeLayout> 
+0

您的布局看起来是正确的。根据您的主题/样式,您可能在按钮上有一些默认边距。尝试在android:layout_marginTop =“14dp”下添加android:layout_margin =“0dp”。此外,你的按钮可绘制,如果它是一个图像,可能在你需要挖掘的PNG空白。 – 2013-05-03 19:38:09

+0

还有一些边框...我已经实现了所有这些方法... – user2229066 2013-05-03 21:18:59

+1

你可以发布截图吗? – 2013-05-03 21:24:31

回答

4

尝试:

android:layout_alignParentLeft="true" 
android:layout_alignParentRight="true" 
+0

我已经尝试过了,但左侧和右侧仍然有一个边框:( – user2229066 2013-05-03 21:16:40

+0

这可能是活动的水平/垂直边距。请检查*/res/values/dimens.xml *文件。 – Voicu 2013-05-03 21:26:49

+0

我已将其更改为0dp页边距然后我该怎么办? – user2229066 2013-05-03 21:47:54

1

你刚刚尝试下面的代码: -

 <Button 
    android:id="@+id/categories" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:layout_below="@+id/textView1" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="14dp" 
    android:background="@drawable/buttonpress" 
    android:onClick="Categories" 
    android:text="@string/title_activity_categories" 
    android:textColor="#228496" 
    android:textSize="21sp" /> 
+0

只需注意,除非您的目标是API 7及更低版本,否则您将因为'fill_parent'在API 8中被弃用,所以想将'fill_parent'切换到'match_parent'。 – TronicZomB 2013-05-03 19:41:14

+0

我已经尝试过了,但仍然有一个非常小的边界,令人沮丧:( – user2229066 2013-05-03 21:16:08

0

我有好运气与Android:layout_weight =布局简单时,相对布局项目中的“1”。

相关问题