【问题标题】:How To Set Width/Height Of An Android Relative Layout Via Programming?如何通过编程设置Android相对布局的宽度/高度?
【发布时间】:2016-01-17 13:53:46
【问题描述】:

我正在使用 Android Studio 开发一个 Android 应用程序,并且只是一个新手。我想在currentscreenwidth + 300dp 上进行外部主布局,高度应为 100% 表示match_parent。在那个主要的外部布局中,我正在创建两个布局。内部第一个布局将有一个固定的 300dp 宽度,另一个布局应该有screenwidth+300dp。为此,我必须主布局宽度currentscreenwidth + 300dp,因为我的内部布局之一已固定300dp,而另一个有match_parent 并排成一排。为此,我使用以下代码,但它不起作用。当我手动将大约 1000dp 的宽度写入外部布局时,我的内部布局可以正常工作,但我想要 currentscreenwidth + 300dp 的修复计算。我该怎么做?

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainbody"
    android:layout_width="match_parent"   //currentscreenwidth + 300dp
    android:layout_height="match_parent"
    tools:context="com.domain.project.MainActivity">

    <RelativeLayout
        android:id="@+id/menubar"
        android:layout_width="300dp"   //300dp
        android:layout_height="fill_parent">
    </RelativeLayout>
    <RelativeLayout
        android:layout_toRightOf="@+id/menubar"
        android:layout_width="match_parent"    //currentscreenwidth
        android:layout_height="fill_parent">
    </RelativeLayout>

</RelativeLayout>

MainActivity.java:

package com.domain.project;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.widget.Button;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get Width And Height Of Screen On Page Load
        Display display = getWindowManager().getDefaultDisplay();
        DisplayMetrics outMetrics = new DisplayMetrics();
        display.getMetrics(outMetrics);
        float density  = getResources().getDisplayMetrics().density;
        float dpHeight = outMetrics.heightPixels / density;
        float dpWidth  = outMetrics.widthPixels / density;

        int width = Math.round(dpWidth);
        RelativeLayout rl = (RelativeLayout) findViewById(R.id.mainbody);
        //rl.getLayoutParams().height = 100;
        rl.getLayoutParams().width = width + 300;

    }


}

【问题讨论】:

    标签: java android xml android-layout android-studio


    【解决方案1】:

    最简单的事情是创建一个自定义ViewGroup

    自己测量和布置孩子。 300dp 的会用MeasureSpec.EXACTLY 调用,屏幕宽度可以直接通过视图组MeasureSpec.AT_MOSTUNSPECIFIED,它应该测量到全屏宽度。

    此示例使用 300 像素,以免进一步复杂化。

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多