【问题标题】:How I set the width of RelativeLayout to screen width?如何将 RelativeLayout 的宽度设置为屏幕宽度?
【发布时间】:2015-07-16 17:25:37
【问题描述】:

我有这个RelativeLayout 组件,我想将它设置为整个屏幕宽度。我该怎么做?

atendente.xml

<?xml version="1.0" encoding="utf-8"?>
<merge
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<RelativeLayout
    style="@style/layoutAtendenteBalao"
    android:id="@+atendente/rltAtendenteBalao">

styles.xml

<style name="layoutAtendenteBalao">
     <item name="android:layout_width">fill_parent</item>
     <item name="android:layout_height">210px</item>
     <item name="android:layout_alignParentRight">true</item>
     <item name="android:layout_alignParentBottom">true</item>
     <item name="android:layout_marginRight">176px</item>
     <item name="android:layout_marginBottom">140px</item>
     <item name="android:background">@drawable/borda_balao</item>
     <item name="android:paddingLeft">15dp</item>
     <item name="android:paddingRight">15dp</item>
     <item name="android:paddingTop">10dp</item>
     <item name="android:paddingBottom">10dp</item>
</style>

我试图把fill_parent 填满整个宽度。但它不起作用。我能做什么?

【问题讨论】:

  • 我看不出布局是如何填满整个屏幕宽度的,你定义了左右内边距和右边距。

标签: android width screen android-relativelayout resolution


【解决方案1】:

为此,您必须在 layout node 中添加 android:layout_widthandroid:layout_height,例如:

<RelativeLayout
    style="@style/layoutAtendenteBalao"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+atendente/rltAtendenteBalao">

【讨论】:

  • @shkschneider 我正在考虑添加这个:)
  • 他不必,他应该这样做。
【解决方案2】:

您应该在将此布局包含到其他布局时设置它,如下所示:

<include android:id=”@+id/your_id”
         android:layout_width=”match_parent”
         android:layout_height=”match_parent”
         layout=”@layout/your_layout”/>

【讨论】:

  • 什么是android:id和布局?我应该在styles.xml 还是atendente.xml 中设置它?
  • 如果我是对的,您将使用您的 布局作为 在其他中,请参见此处的示例:developer.android.com/training/improving-layouts/…。因此,如果需要,您应该输入此包含的 @+id,并且布局属性是您的 .xml 文件的名称
【解决方案3】:

对 fill_parrent 和 wrap_content 使用 -1 和 -2

参考:http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#FILL_PARENT

<style name="layoutAtendenteBalao">
     <item name="android:layout_width">-1</item>
     <item name="android:layout_height">210px</item>
     <item name="android:layout_alignParentRight">true</item>
     <item name="android:layout_alignParentBottom">true</item>
     <item name="android:layout_marginRight">176px</item>
     <item name="android:layout_marginBottom">140px</item>
     <item name="android:background">@drawable/borda_balao</item>
     <item name="android:paddingLeft">15dp</item>
     <item name="android:paddingRight">15dp</item>
     <item name="android:paddingTop">10dp</item>
     <item name="android:paddingBottom">10dp</item>
</style>

【讨论】:

    猜你喜欢
    • 2014-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多