【问题标题】:How can I combine Relative and Linear to achieve this XML?如何结合相对和线性来实现这个 XML?
【发布时间】:2017-03-26 18:21:06
【问题描述】:

我想创建

Layout 出现在所附图像中,但我不能,或者它移动或重叠,我不适合完美地玩宽度和高度。我想要一个水平的listview,我将把它放在一个LinerLayout 里面,它是红色的,4 个橙色的矩形(我试图成为Relative 将是按钮或imagebutton我会放进去,但我一直没能做到。

我尝试将FrameLayout 放入其中,我已经可以使用Relative 进行更多操作,但我总是将自己放在左上角,无法将它们向右或向下移动,并且我希望一切都是方形的。有人做过类似或类似的事情吗?

我的代码,但我无法使用橙色和红色矩形实现我的图像。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:stretchColumns="1">


<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="111dp"
        android:orientation="horizontal"></LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="173dp">

    </RelativeLayout>
</FrameLayout>
</RelativeLayout>

【问题讨论】:

  • 发布您的代码。
  • 我的代码没用,因为你可以读到这个 ​​xml 对我来说是不可能的,所以我只能将 1 linearlayout 和 1 framelayoutRelativeLaoyut 放在一起,但别担心,现在我用我的代码更新我的问题.....
  • @Rasi 我已经更新了我的代码,请帮我解决我的问题

标签: java android xml android-layout android-relativelayout


【解决方案1】:

试试这个布局:

 <?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:orientation="vertical"
    android:padding="5dp"
    android:weightSum="5">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@drawable/border"
        android:orientation="horizontal">

        <ListView
            android:id="@+id/list_item"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></ListView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="4"
        android:orientation="vertical">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:orientation="horizontal"
            android:weightSum="2">


            <ImageButton
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/border"
                android:src="@mipmap/ic_launcher" />

            <ImageButton
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/border"
                android:src="@mipmap/ic_launcher" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:orientation="horizontal"
            android:weightSum="2">


            <ImageButton
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/border"
                android:src="@mipmap/ic_launcher" />

            <ImageButton
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/border"
                android:src="@mipmap/ic_launcher" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

在 res/drawable/border.xml 中

 <?xml version="1.0" encoding="utf-8"?><!--  res/drawable/rounded_edittext.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="10dp"
    android:shape="rectangle">

    <stroke
        android:width="3dp"
        android:color="#000000" />

    <corners android:radius="0dp" />
</shape>

输出

【讨论】:

  • 您已经使用stroke 放置了1 个背景,但是如果我想在所有4 个图像按钮中使用1 个不同的背景颜色,我可以使用不同的背景颜色放置4 个borders.xml 吗?
  • 是的,您可以...您必须更改笔触颜色并为每个图像按钮应用不同的可绘制背景
  • 一个问题,如果我想在listview上面放1个textview,我只能把textview放在上面就可以了吗?但是我不能这样做..T.T
  • 确保您可以在顶部添加 textview 只需将第一个 Linearlayout 方向更改为垂直 android:orientation="vertical"...并添加 textview
  • 好的,完美!谢谢大家,你简直是最好的哈哈:)
【解决方案2】:

将相对布局设为父级。

<RelativeLayout android:layout_width="match_parent"
    android:layout_height="wrap_content">

  <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation=horizontal"
    android:alignParentTop=""
    android:id="llTopH".....>
       <your code for top layout>
  </LinearLayout>


  <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation=horizontal"
    android:layout_below="llTopH"
    android:weightSum = "2"
    android:id="llTopH2".....>

     <LineraLayout
        android:layout_width="0dp"
       android:weight = "1"
    android:layout_height="wrap_content"
      />

  <LineraLayout
        android:layout_width="0dp"
       android:weight = "1"
    android:layout_height="wrap_content"
      />

this will two layouts on 2nd line

 </LinearLayout>

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation=horizontal"
    android:layout_below="llTopH2"
    android:weightSum = "2"
    android:id="llTopH3".....>

      <LineraLayout
        android:layout_width="0dp"
       android:weight = "1"
    android:layout_height="wrap_content"
      />

      <LineraLayout
        android:layout_width="0dp"
       android:weight = "1"
    android:layout_height="wrap_content"
      />

this will two layouts on 3rd line

 </LinearLayout>

</RelativeLayout>     

【讨论】:

    【解决方案3】:
    <?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:orientation="vertical">
    
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1"
          android:orientation="horizontal">
    
        <ListView
            android:id="@+id/listview"
            android:rotation="-90"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
        </ListView>
      </LinearLayout>
    
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="4"
          android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:orientation="horizontal"
            android:weightSum="2">
    
    
          <ImageButton
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_weight="1"
              android:src="@mipmap/ic_launcher" />
    
          <ImageButton
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_weight="1"
    
              android:src="@mipmap/ic_launcher" />
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:orientation="horizontal"
            android:weightSum="2">
    
    
          <ImageButton
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_weight="1"
              android:src="@mipmap/ic_launcher" />
    
          <ImageButton
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_weight="1"
              android:src="@mipmap/ic_launcher" />
        </LinearLayout>
      </LinearLayout>
    <
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-08
      • 1970-01-01
      • 2019-04-15
      • 2015-08-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多