【发布时间】: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 时,“红色方块”消失了。我忘了说第二张照片是蒙太奇。