【问题标题】:android studio relativelayoutandroid studio 相对布局
【发布时间】:2022-01-17 09:36:03
【问题描述】:

Android Studio 和 Java 新手。我正在寻找使用相对布局来产生这个: - Layout required

由于某种原因无法上班。如果布局发生变化,即向右旋转 90 度。这使顶部的图像及其下方的四个按钮起作用。但无法通过右侧按钮获取左侧图像。

请帮忙。

【问题讨论】:

  • 请提供一些代码。

标签: java android android-layout


【解决方案1】:

您可以使用 LinearLayout 代替 relativelayout。

<?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">
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">
        <Button
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="button 1" />
        <Button
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="button 2" />
        <Button
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="button 3" />
        <Button
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="button 4" />
    </LinearLayout>
</LinearLayout>

【讨论】:

    【解决方案2】:

    使用LinearLayout 比Relative 更容易生成该布局,如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 
    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"
    tools:context=".MainActivity">
    
    <LinearLayout
       android:layout_height="match_parent"
       android:layout_width="match_parent"
       android:weightSum="2">
       <LinearLayout
           android:layout_width="wrap_content"
           android:layout_height="match_parent"
           android:padding="20dp"
           android:layout_weight="1">
           <Button
               android:layout_width="wrap_content"
               android:layout_height="match_parent"
               android:layout_alignParentLeft="true"
               android:text="Image"
               android:layout_weight="1">
           </Button>
       </LinearLayout>
      
       <LinearLayout
           android:layout_width="wrap_content"
           android:layout_height="match_parent"
           android:layout_weight="1"
           android:orientation="vertical"
           android:weightSum="4"
           android:padding="20dp">
           <Button
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:text="Button"
               android:layout_weight="1"></Button>
           <Button
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:text="Button"
               android:layout_weight="1"></Button>
           <Button
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:text="Button"
               android:layout_weight="1"></Button>
           <Button
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:text="Button"
               android:layout_weight="1"></Button>
       </LinearLayout>
    
      </LinearLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-24
      • 1970-01-01
      相关资源
      最近更新 更多