【问题标题】:What is the appropriate here linear layout or relative layout什么是合适的线性布局或相对布局
【发布时间】:2018-06-23 05:54:38
【问题描述】:

请查看随附的屏幕截图,它的简单布局设计我想在其中保持进度条和两个文本视图在中心。我使用线性布局作为父级并将布局重力设置为中心,因此我希望所有视图都应该是线性的中心。 XML 图形预览提供了预期的输出,但是如果我将其闪烁到设备上,底部会有一个间隙。我试图理解可能是什么原因,你能帮我这个设计有什么问题吗?在这种情况下我是否应该考虑相对布局,为什么我不能通过线性布局来实现这一点?

这里是代码 -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_progress"
android:orientation="vertical"
android:gravity="center">


<ProgressBar
    android:id="@+id/progressBar2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/reg_dimen_12dp" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/sync_data_title"
    android:textColor="@color/white"
    android:textStyle="bold" />


<TextView
    android:id="@+id/textView9"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dp"
    android:layout_marginRight="50dp"
    android:layout_marginTop="@dimen/reg_dimen_24dp"
    android:gravity="center_horizontal"
    android:text="@string/sync_data_desc"
    android:textColor="@color/white" />
   </LinearLayout>

下面是我正在尝试的布局

在设备上,如下所示,

【问题讨论】:

  • 显示你的xml代码
  • 发布您的 xml 代码
  • 你能添加你的布局文件吗
  • 嗨@Niranj Patel,请查找 XML 代码
  • 发布您在线性布局中使用的可绘制 bg_progress

标签: android android-layout android-linearlayout android-view android-relativelayout


【解决方案1】:

使用RelativeLayout尝试这种方式修复

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@drawable/bg_progress"
    android:layout_height="match_parent">

    <ProgressBar
        android:id="@+id/progressBar2"
        android:layout_width="wrap_content"
        android:layout_above="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/reg_dimen_12dp" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/sync_data_title"
        android:layout_centerInParent="true"
        android:textColor="@color/white"
        android:textStyle="bold" />


    <TextView
        android:id="@+id/textView9"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="@dimen/reg_dimen_24dp"
        android:gravity="center_horizontal"
        android:text="@string/sync_data_desc"
        android:textColor="@color/white" />
</RelativeLayout>

【讨论】:

  • 你确定它会使用RelativeLayout解决吗?为什么不使用 LinearLayout?
  • @NiranjPatel 是的,我在 linerlayout 上遇到过同样的问题
  • 我也有同样的问题,为什么不用 linearlayout 。它适用于RelativeLayout
  • @chethan 我对此进行了太多研究,但我找不到为什么 linerlayout 行为怪异
  • @MilanPansuriya,没关系,感谢您的时间,但我知道一件事,我们内部的视图没有定义大小,进度条,文本视图是用包装内容定义的。所以系统不知道它的大小来正确放置它,这可能是这种不合适的原因。这是我的理解,
【解决方案2】:

您也可以使用可绘制的形状而不是大图像作为背景。

这是您的背景的示例形状,并在 LinearLayout 的背景中使用它。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:endColor="#EB7C97"
        android:startColor="#ED94A9"
        android:type="linear" />
</shape>

【讨论】:

  • 嗨@Niranj Patel,但实际上图像不是计划中的,它是别的东西,但它具有相同的1440 * 2560分辨率。我已删除该图像以避免在此处发布。
  • 形状可绘制仍然有问题?不工作然后尝试颜色,如果仍然不能使用背景颜色,那么一定是问题
  • 是活动还是片段?
  • 问题仍然存在,我尝试使用上述形状 drawable 。现在只有 1/3 的屏幕呈现。这真的很棘手
  • 它唯一的片段
【解决方案3】:

另一种方法:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_progress">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center_horizontal"
        android:layout_centerInParent="true">


        <ProgressBar
            android:id="@+id/progressBar2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/reg_dimen_12dp" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/sync_data_title"
            android:textColor="@color/white"
            android:textStyle="bold" />


        <TextView
            android:id="@+id/textView9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp"
            android:layout_marginTop="@dimen/reg_dimen_24dp"
            android:text="@string/sync_data_desc"
            android:textAlignment="center"
            android:textColor="@color/white" />
    </LinearLayout>
</RelativeLayout>

【讨论】:

  • of-curse 这会起作用的。但是对我的帖子有什么理解以及为什么 LinearLayout 或 RelativeLayout 的行为不符合我们的预期?
猜你喜欢
  • 2019-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多