【发布时间】: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