简体   繁体   English

如何在另一个布局中嵌入布局?

[英]How to embed a layout inside another layout?

I want a layout similar to below image, where (2) is a LinearLayout and (1) is whatever it can be to make this possible.我想要一个类似于下图的布局,其中 (2) 是 LinearLayout 而 (1) 是使这成为可能的任何东西。 For example imagine (2) is a button configuration and (1) is some text in different sizes and needs to go around (2).例如想象(2)是一个按钮配置,(1)是一些不同大小的文本,需要 go 围绕(2)。

small square inside (bottom right) big square里面的小方块(右下) 大方块

Seems folks misunderstand my question even with the image demonstration, (2) is not on top of (1):!!即使有图像演示,似乎人们也误解了我的问题,(2)不在(1)之上:! Let me add a more detailed image as below:让我添加一个更详细的图像,如下所示:

embedded not on top of嵌入不在顶部

Its pretty easy, Have you done Constraint Layout?很简单,你做过约束布局吗? try with this.试试这个。

 <?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>

Please try with this code (Paste this code in xml layout file) and get the result.请尝试使用此代码(将此代码粘贴到 xml 布局文件中)并获得结果。 Note: You have to define constrains as per given here.注意:您必须按照此处给出的定义约束。

I suggest you use include .我建议你使用include Note that if you include android:id... into the <include /> tag, it will override whatever id was defined inside the included layout.请注意,如果您将android:id...包含到<include />标记中,它将覆盖在包含的布局中定义的任何 id。 For example:例如:

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

yourlayout.xml:你的布局.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>

Then you would reference this included layout in code as follows:然后,您将在代码中引用此包含的布局,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM