對不起,我可能愚蠢的問題,但我是一個初學者。 我有「相對」正方形佈局,我需要在這個正方形佈局中插入按鈕,這個佈局的大小和位置相對變化。這些參數將取決於正方形佈局的尺寸。在第一張照片是一個方形佈局的嘗試,我想放置按鈕。第二張圖片顯示可能看起來像結果。 Sry for my english :)相對放置按鈕的方形佈局 - Android
謝謝您的意見。
這是我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>
和你真的想要什麼? – Lal
嘗試使用RelativeLayout作爲您的按鈕想要移動的表面。這樣,您可以通過設置上下左右邊距來輕鬆更改位置。 – Krish
同意克里什改爲RelativeLayout,然後只需拖放一個按鈕小部件多數民衆贊成在所有:) – BiggDawgg