【问题标题】:Square layout with relatively placed buttons - Android带有相对放置按钮的方形布局 - Android
【发布时间】:2015-01-06 18:25:13
【问题描述】:

抱歉我的问题可能很愚蠢,但我是初学者。 我有“相对”的方形布局,我需要在这个方形布局中插入按钮,它相对改变了大小和位置。这些参数将取决于方形布局的尺寸。在第一张图片上是方形布局的尝试,我想在其中放置按钮。在第二张图片上显示可能看起来像结果。对不起我的英语:)

感谢您的 cmets。

这是我的 squarelayout.java

package com.example.squarelayout;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

public class SquareView extends View {
  public SquareView(Context context) {
    super(context);
  }

  public SquareView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public SquareView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  }

  public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int size = Math.min(getMeasuredWidth(), getMeasuredHeight());
    setMeasuredDimension(size, size);
  }
}

这是我的 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="top"
    android:orientation="horizontal"
    android:weightSum="100">


      <com.example.squarelayout.SquareView
          android:layout_width="0dp"
          android:layout_height="fill_parent"
          android:layout_weight="100"
          android:background="#f00"
          android:orientation="horizontal" />

  </LinearLayout>

</FrameLayout>

【问题讨论】:

  • 你真正想要什么???
  • 尝试使用RelativeLayout 作为按钮想要移动的表面。这样您就可以通过设置 margin top 和 left 轻松更改位置。
  • 同意 Krish 更改为 RelativeLayout,然后只需拖放一个按钮小部件即可:)
  • 当我使用 RelativeLayout 时,“红色方块”消失了。我忘了说第二张照片是蒙太奇。

标签: android android-layout


【解决方案1】:

将您的 SquareView 类更改为扩展 RelativeLayout 而不是 View

如果您像下面这样更改您的 activity_main.xml,删除 LinearLayout 并将 SquareView 的宽度和高度设置为 fill_parent,那么它不会使正方形消失:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >

          <com.example.transparentcircle.SquareView
          android:id="@+id/squareview"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:background="#f00">

              <Button
                  android:id="@+id/button1"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_marginLeft="160dp"
                  android:layout_marginTop="40dp"
                  android:text="B"/>

              <Button
                  android:id="@+id/button2"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_marginLeft="120dp"
                  android:layout_marginTop="100dp"
                  android:text="B"/>

          </com.example.transparentcircle.SquareView>

</FrameLayout>

按钮可以像上面那样添加到xml中的SquareView,也可以通过编程方式创建。

您还可以根据SquareView 的大小在代码中设置边距。但是,您不能直接在主活动的 onCreate() 中执行此操作,而是需要等到布局设置完成后。有关如何执行此操作的示例,请参阅 this question

另外,请参阅this question 了解如何在代码中设置边距。

【讨论】:

    猜你喜欢
    • 2011-02-26
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多