【问题标题】:android layout image at left and two text block at right左侧的android布局图像和右侧的两个文本块
【发布时间】:2016-12-29 02:41:07
【问题描述】:

我想要这样的布局 Layout where an image at the bottom and two text block at right

这是一个非常简单的布局,左边有一个图像,右边有两个文本块。我知道如何在 html 和 CSS 中执行此操作,但不知道如何在 XML 中执行此操作。向左或向右浮动元素的属性是什么?现在如果我添加一个图像和两个文本块,第二个文本块不会显示。

【问题讨论】:

    标签: android xml layout android-linearlayout android-layout-weight


    【解决方案1】:

    试试这个代码,我已经测试过了:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:layout_gravity="center"
        android:layout_weight="1"/>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"/>
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:layout_weight="1"
            android:gravity="center"/>
    
    </LinearLayout>
    

    【讨论】:

    • imageView 不必在 LinearLayout 中。您可以只删除父 linearLayout 并将 weight-property 赋予 imageView。
    【解决方案2】:
    <?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="horizontal">
    
    <LinearLayout
       android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    
    </LinearLayout>
    
        <ImageView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent" />
    </LinearLayout>
    

    自行编辑 :)

    【讨论】:

      【解决方案3】:

      您可以为此使用相对布局:

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_height="wrap_content"
                  android:layout_width="match_parent">
          <ImageView android:id="@+id/img" 
                 android:layout_height="wrap_content"
                 android:layout_width="wrap_content" 
                 android:src="@drawable/ic_clear"/>
          <TextView android:id="@+id/text1"
                android:layout_height="wrap_content"
                android:layout_toEndOf="@+id/img"
                android:layout_toRightOf="@+id/img"
                android:layout_width="match_parent"
                android:text="text 1"/>
          <TextView android:layout_below="@+id/text1"
                android:layout_height="wrap_content"
                android:layout_toEndOf="@+id/img"
                android:layout_toRightOf="@+id/img"
                android:layout_width="match_parent"
                android:text="text 2"/>
      </RelativeLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-19
        • 2016-03-11
        • 1970-01-01
        • 1970-01-01
        • 2016-07-11
        • 2012-02-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多