【问题标题】:RelativeLayout inside RelativeLayout is not aligned left completelyRelativeLayout 内部的 RelativeLayout 未完全左对齐
【发布时间】:2014-10-03 22:57:40
【问题描述】:

我在另一个 relativelayout 中有一个 relativelayout,里面有 4 个按钮。我想将内部 relativelayout 对齐到父 relativelayout 的左侧,但它没有完全对齐,内部视图的左侧与父视图的左侧之间有一个空白空间,这是 xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="raw4.kaw.mp2.MainActivity"
android:background="@color/background_all" >

<RelativeLayout 
    android:id="@+id/main_btn_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
    
<Button
    android:id="@+id/btn_main_news"
    android:layout_width="fill_parent"
    android:layout_height="60dp"
    android:text="@string/btn_news_text"
    android:layout_marginTop="10dp"
    android:layout_alignParentTop="true"
    android:background="@color/background_btn"
    android:textColor="@color/text_color_btn" />

<Button
    android:id="@+id/btn_main_products"
    android:layout_width="fill_parent"
    android:layout_height="60dp"
    android:text="@string/btn_products_text"
    android:layout_below="@id/btn_main_news"
    android:layout_marginTop="10dp"
    android:background="@color/background_btn"
    android:textColor="@color/text_color_btn" />

<Button
    android:id="@+id/btn_main_aboutus"
    android:layout_width="fill_parent"
    android:layout_height="60dp"
    android:text="@string/btn_aboutus_text"
    android:layout_below="@id/btn_main_products"
    android:layout_marginTop="10dp"
    android:background="@color/background_btn"
    android:textColor="@color/text_color_btn" />

<Button
    android:id="@+id/btn_main_contactus"
    android:layout_width="fill_parent"
    android:layout_height="60dp"
    android:text="@string/btn_contactus_text"
    android:layout_below="@id/btn_main_aboutus"
    android:layout_marginTop="10dp"
    android:background="@color/background_btn"
    android:textColor="@color/text_color_btn" />

</RelativeLayout>

这是 MainActivity.java 中添加对齐规则后将内部布局宽度减小到 80% 的代码:

DisplayMetrics metrics = this.getResources().getDisplayMetrics();
    int dpi = metrics.widthPixels;
    int dpiPerc = (int)(dpi * 0.8);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            dpiPerc,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
    params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
    RelativeLayout main_rl_container = (RelativeLayout)findViewById(R.id.main_btn_container);
    main_rl_container.setLayoutParams(params);

但这是结果:

如您所见,内部布局的左侧有一个间隙。谢谢你的帮助。

【问题讨论】:

  • 请从父级相对布局中删除 android:paddingLeft="@dimen/activity_horizo​​ntal_margin"。

标签: android android-layout alignment android-relativelayout


【解决方案1】:
android:paddingLeft="@dimen/activity_horizontal_margin"

因为尺寸。 你do not need to remove these Lines

但转到 res 文件夹 > Values > dimen.xml 。 这里默认为activity_horizontal_margin指定了一些空间分配,将其更改为0dp

如果您删除该行,那很好,但是,您将在要创建的每个 XML 文件中遇到这些问题。所以我认为这个解决方案更好。

【讨论】:

    【解决方案2】:

    由于在根 RelativeLayout 中设置的填充,我觉得很奇怪。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_main_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="raw4.kaw.mp2.MainActivity"
    android:background="@color/background_all" >
    

    【讨论】:

      【解决方案3】:

      复制粘贴这段代码会解决你的问题

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/main_main_container"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context="raw4.kaw.mp2.MainActivity"
      android:background="@color/background_all" >
      
      <RelativeLayout 
          android:id="@+id/main_btn_container"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          >
      
      <Button
          android:id="@+id/btn_main_news"
          android:layout_width="fill_parent"
          android:layout_height="60dp"
          android:text="@string/btn_news_text"
          android:layout_marginTop="10dp"
          android:layout_alignParentTop="true"
          android:background="@color/background_btn"
          android:textColor="@color/text_color_btn" />
      
      <Button
          android:id="@+id/btn_main_products"
          android:layout_width="fill_parent"
          android:layout_height="60dp"
          android:text="@string/btn_products_text"
          android:layout_below="@id/btn_main_news"
          android:layout_marginTop="10dp"
          android:background="@color/background_btn"
          android:textColor="@color/text_color_btn" />
      
      <Button
          android:id="@+id/btn_main_aboutus"
          android:layout_width="fill_parent"
          android:layout_height="60dp"
          android:text="@string/btn_aboutus_text"
          android:layout_below="@id/btn_main_products"
          android:layout_marginTop="10dp"
          android:background="@color/background_btn"
          android:textColor="@color/text_color_btn" />
      
      <Button
          android:id="@+id/btn_main_contactus"
          android:layout_width="fill_parent"
          android:layout_height="60dp"
          android:text="@string/btn_contactus_text"
          android:layout_below="@id/btn_main_aboutus"
          android:layout_marginTop="10dp"
          android:background="@color/background_btn"
          android:textColor="@color/text_color_btn" />
      
      </RelativeLayout>
      

      你的问题是你的父布局使用android:paddingLeft="@dimen/activity_horizontal_margin",这就是为什么不能完全删除这一行,你会得到你的解决方案,你也可以复制粘贴我上面的代码:)

      【讨论】:

        【解决方案4】:

        RelativeLayout上有一个填充。

        删除以下行:

        android:paddingLeft="@dimen/activity_horizontal_margin"
        

        【讨论】:

        • 是的,这解决了问题,但 Android 的一切都是为了更快:) 仍然是一个赞成票。还是谢谢
        • @MiroMarkarian 兄弟从我这里获得一票 :) 你应该得到它
        • @Androidiseverythingforme 谢谢。 :)
        • 对不起,伙计,我不知道为什么他的答案比你的早,这也发生过好几次了.. 无论如何抱歉,投票给你
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-07-07
        • 1970-01-01
        • 1970-01-01
        • 2012-12-13
        • 1970-01-01
        • 2023-03-18
        • 2015-09-14
        相关资源
        最近更新 更多