2015-01-06 28 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瞭解如何設置代碼邊距。