Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.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
Java 如何通过编程设置Android相对布局的宽度/高度?_Java_Android_Xml_Android Layout_Android Studio - Fatal编程技术网

Java 如何通过编程设置Android相对布局的宽度/高度?

Java 如何通过编程设置Android相对布局的宽度/高度?,java,android,xml,android-layout,android-studio,Java,Android,Xml,Android Layout,Android Studio,我正在使用Android Studio开发一个Android应用程序,这只是一个新的应用程序。我想在currentscreenwidth+300dp上进行外部主布局,高度应为100%表示匹配父项。在这个主要的外部布局中,我创建了两个布局。第一个内部布局将具有固定的300dp宽度,而另一个布局应具有屏幕宽度+300dp。为此,我必须调整主布局宽度currentscreenwidth+300dp,因为我的一个内部布局固定了300dp,另一个布局匹配父布局并放置在一行中。为此,我使用以下代码,但它不工

我正在使用Android Studio开发一个Android应用程序,这只是一个新的应用程序。我想在
currentscreenwidth+300dp
上进行外部主布局,高度应为100%表示
匹配父项。在这个主要的外部布局中,我创建了两个布局。第一个内部布局将具有固定的300dp宽度,而另一个布局应具有
屏幕宽度+300dp
。为此,我必须调整主布局宽度
currentscreenwidth+300dp
,因为我的一个内部布局固定了
300dp
,另一个布局匹配父布局并放置在一行中。为此,我使用以下代码,但它不工作。当我手动将宽度约为1000dp写入外部布局时,我的内部布局工作正常,但我需要的是
currentscreenwidth+300dp
的固定计算。我该怎么做

activity_main.xml:
最简单的方法是创建一个自定义的
视图组

自己测量和布置孩子们。300dp的一个会被称为
MeasureSpec.确切地说
,屏幕宽度的一个你可以通过你的视图组
MeasureSpec.最多
未指定
,它应该测量到整个屏幕的宽度

此示例使用300px以避免使其进一步复杂化

public class SomeLayout extends ViewGroup {

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        getChildAt(0).measure(MeasureSpec.makeMeasureSpec(300, MeasureSpec.EXACTLY), heightMeasureSpec);
        getChildAt(1).measure(widthMeasureSpec, heightMeasureSpec);

        setMeasuredDimension(getChildAt(0).getMeasuredWidth() + getChildAt(1).getMeasuredWidth(),
                Math.max(getChildAt(0).getMeasuredHeight(), getChildAt(1).getMeasuredHeight()));
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        getChildAt(0).layout(l, t, l + getChildAt(0).getMeasuredWidth(), b);
        getChildAt(1).layout(l + getChildAt(0).getMeasuredWidth(), t,
                l + getChildAt(0).getMeasuredWidth() + getChildAt(1).getMeasuredWidth(), b);
    }
}
像这样使用它

<SomeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorAccent"></RelativeLayout>


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimaryDark"></RelativeLayout>
</SomeLayout>

public class SomeLayout extends ViewGroup {

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        getChildAt(0).measure(MeasureSpec.makeMeasureSpec(300, MeasureSpec.EXACTLY), heightMeasureSpec);
        getChildAt(1).measure(widthMeasureSpec, heightMeasureSpec);

        setMeasuredDimension(getChildAt(0).getMeasuredWidth() + getChildAt(1).getMeasuredWidth(),
                Math.max(getChildAt(0).getMeasuredHeight(), getChildAt(1).getMeasuredHeight()));
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        getChildAt(0).layout(l, t, l + getChildAt(0).getMeasuredWidth(), b);
        getChildAt(1).layout(l + getChildAt(0).getMeasuredWidth(), t,
                l + getChildAt(0).getMeasuredWidth() + getChildAt(1).getMeasuredWidth(), b);
    }
}
<SomeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorAccent"></RelativeLayout>


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimaryDark"></RelativeLayout>
</SomeLayout>