【问题标题】:How to embed a layout inside another layout?如何在另一个布局中嵌入布局?
【发布时间】:2020-06-08 12:12:09
【问题描述】:

我想要一个类似于下图的布局,其中 (2) 是一个 LinearLayout 而 (1) 是使这成为可能的任何东西。 例如假设 (2) 是一个按钮配置, (1) 是一些不同大小的文本,需要绕过 (2)。

small square inside (bottom right) big square

似乎人们即使通过图像演示也误解了我的问题,(2)不在(1)之上!让我添加一个更详细的图像如下:

embedded not on top of

【问题讨论】:

  • 您的问题是“如何在图像周围环绕文字”?

标签: java android xml layout


【解决方案1】:

我建议你使用include。请注意,如果您将android:id... 包含到<include /> 标记中,它将覆盖在包含的布局中定义的任何ID。例如:

<include
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:id="@+id/some_id_if_needed"
   layout="@layout/yourlayout" />

你的布局.xml:

<LinearLayout
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:id="@+id/some_other_id">
   <Button
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:id="@+id/button1" />
 </LinearLayout>

然后你会在代码中引用这个包含的布局,如下所示:

View includedLayout = findViewById(R.id.some_id_if_needed);
Button insideTheIncludedLayout = (Button)includedLayout.findViewById(R.id.button1);

【讨论】:

  • 根本没用。我猜你没听懂我的问题。
【解决方案2】:

很简单,你做过约束布局吗?试试这个。

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

   <androidx.constraintlayout.widget.ConstraintLayout
       android:layout_width="match_parent"
       android:layout_height="250dp">

      <ImageView
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:src="@mipmap/ic_launcher"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent" />


      <ImageView
          android:layout_width="80dp"
          android:layout_height="80dp"
          android:src="@mipmap/ic_launcher"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="1.0"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintVertical_bias="1.0" />


   </androidx.constraintlayout.widget.ConstraintLayout>



</LinearLayout>

请尝试使用此代码(将此代码粘贴到 xml 布局文件中)并获得结果。 注意:您必须按照此处给出的定义约束。

【讨论】:

  • 我曾经使用过约束布局,但是你会怎么做呢?据我所知,没有任何相关选项。
  • 我的朋友根本不是我问的!我没有说(2)必须在(1)之上!这也可以通过简单的相对布局来完成。想象你的第一张图片是文本,那么你的第二张图片不应该放在文本之上。但文字应该围绕图像。
猜你喜欢
  • 1970-01-01
  • 2021-08-10
  • 2021-09-23
  • 2018-09-11
  • 1970-01-01
  • 2011-10-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-09
相关资源
最近更新 更多