2015-01-06 26 views
0

对不起,我可能愚蠢的问题,但我是一个初学者。 我有“相对”正方形布局,我需要在这个正方形布局中插入按钮,这个布局的大小和位置相对变化。这些参数将取决于正方形布局的尺寸。在第一张照片是一个方形布局的尝试,我想放置按钮。第二张图片显示可能看起来像结果。 Sry for my english :)相对放置按钮的方形布局 - Android

谢谢您的意见。

first second

这是我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> 
+1

和你真的想要什么? – Lal

+0

尝试使用RelativeLayout作为您的按钮想要移动的表面。这样,您可以通过设置上下左右边距来轻松更改位置。 – Krish

+0

同意克里什改为RelativeLayout,然后只需拖放一个按钮小部件多数民众赞成在所有:) – BiggDawgg

回答

0

更改您的SquareView类来扩展RelativeLayout,而不是View

如果您改变activity_main.xml中像下面,去除LinearLayout并设置SquareViewfill_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了解如何设置代码边距。