Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
根据android屏幕上的坐标计算边距_Android_Android Layout - Fatal编程技术网

根据android屏幕上的坐标计算边距

根据android屏幕上的坐标计算边距,android,android-layout,Android,Android Layout,我有一个库,可以在布局中插入浮动按钮() 然后,我有这样的布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:fontawesometext="http://schemas.android.com/apk/res-auto" android:orientation="ve

我有一个库,可以在布局中插入浮动按钮()

然后,我有这样的布局:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fontawesometext="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="32dp"
        android:layout_marginBottom="16dp"
        android:layout_marginRight="16dp">

        <com.beardedhen.androidbootstrap.FontAwesomeText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="4dp"
            android:textSize="45sp"
            android:textColor="#737373"
            android:id="@+id/faIcon"
            fontawesometext:fa_icon="fa-question" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text='"texto de ejemplo de pregunta"'
            android:id="@+id/tvQuestion"
            android:textColor="@color/primary"
            android:textStyle="bold"
            android:textSize="28sp"
            android:typeface="monospace"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="32dp"
            android:layout_marginBottom="16sp" />
    </LinearLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/frameConainter"
        android:background="@color/dark_grey" />

</LinearLayout>
但是我不知道如何根据坐标计算边距,以便将视图设置在我想要的位置


任何人都知道有什么更好的方法可以做到这一点?(我不能使用XML布局…)

为什么不简单地设置边距按钮(顶部=框架布局高度-按钮高度/2,左侧=设备宽度-按钮宽度-40,右侧=10)。lp=btn.getLayoutParam()然后lp.marginTop=…这不起作用,我无法获取framelayout的高度,它始终为0..您应该在onWindowFocusChanged()方法中获取它的高度。在此之前,未创建视图,因此其高度为0
public class YesNoReplyView extends ReplyParentView {

    private FloatingActionButton buttonYes;
    private FloatingActionButton buttonNo;

    public YesNoReplyView(Context context) {
        super(context);
    }

    public YesNoReplyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public YesNoReplyView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void initializeComponents() {

        int[] position = new int[2];
        getLocationOnScreen(position);

        buttonYes = new FloatingActionButton.Builder((Activity)getContext())
                .withDrawable(getResources().getDrawable(R.drawable.ic_plus))
                .withButtonColor(getResources().getColor(R.color.green_success))
                .withGravity(Gravity.BOTTOM | Gravity.RIGHT)
                .withMargins(0, 0, 16, 16)
                .create();

        buttonNo = new FloatingActionButton.Builder((Activity)getContext())
                .withDrawable(getResources().getDrawable(R.drawable.ic_plus))
                .withButtonColor(getResources().getColor(R.color.primary))
                .withGravity(Gravity.BOTTOM | Gravity.RIGHT)
                .withMargins(0, 0, 26, 26)
                .create();

    }


    @Override
    public String getResult() {
        String result = "";
        return result;
    }
}