Android 如何在不透支其他元素的情况下填充布局中的剩余空间?
我创建了一个新的小例子来解释我的新问题。在这个例子中,我创建了一个RelativeLayout,它填充了整个屏幕。和3名儿童:Android 如何在不透支其他元素的情况下填充布局中的剩余空间?,android,android-layout,Android,Android Layout,我创建了一个新的小例子来解释我的新问题。在这个例子中,我创建了一个RelativeLayout,它填充了整个屏幕。和3名儿童: 自定义视图(PView)创建用于绘制比例视图,该视图必须在纵向模式下按宽度填充所有可用空间,在横向模式下按高度填充所有可用空间,且框架布局(粉色)的顶部和底部不重叠 id为top的FrameLayout必须显示在中间上方(红色) 底部id为的FrameLayout必须显示在中间下方(黄色) 但现在在横向模式中,中间项透支了顶部和底部。有人知道怎么修吗 布局: <?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/top"
android:layout_width="100dp"
android:layout_height="24dp"
android:layout_above="@+id/middle"
android:layout_centerHorizontal="true"
android:background="#FFFF0000" />
<test.com.heighttest.PView
android:id="@+id/middle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:background="#FFFF00FF" />
<FrameLayout
android:id="@+id/bottom"
android:layout_width="100dp"
android:layout_height="24dp"
android:layout_below="@+id/middle"
android:layout_centerHorizontal="true"
android:background="#FFFFFF00" />
</RelativeLayout>
MainActivity.java
package test.com.heighttest;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.RelativeLayout;
/**
* Created by Anton Potekhin (Anton.Potekhin@gmail.com) on 26.09.16.
*/
public class PView extends RelativeLayout {
public static final int USE_FOR_SCALE_WIDTH = 0;
public static final int USE_FOR_SCALE_HEIGHT = 1;
public int getUseForScale() {
return useForScale;
}
public void setUseForScale(int useForScale) {
this.useForScale = useForScale;
}
private int useForScale = 0;
public float getAspectRatio() {
return aspectRatio;
}
public void setAspectRatio(float aspectRatio) {
this.aspectRatio = aspectRatio;
}
/**
* Aspect ratio to calculate size of view. Set 0 to not use aspect ratio (Works like RelativeLayout).
*/
private float aspectRatio = 0;
public PView(Context context) {
super(context);
}
public PView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//Use aspect ratio only if it more than 0
if (aspectRatio > 0) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
if (useForScale == USE_FOR_SCALE_WIDTH) {
height = Math.round(width / aspectRatio);
} else {
width = Math.round(height * aspectRatio);
}
widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
package test.com.heighttest;
import android.content.res.Configuration;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
private PView middleView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
com.facebook.stetho.Stetho.initializeWithDefaults(this);
setContentView(R.layout.activity_main);
middleView = (PView) findViewById(R.id.middle);
middleView.setAspectRatio(1.2f);
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
middleView.setUseForScale(PView.USE_FOR_SCALE_HEIGHT);
} else {
middleView.setUseForScale(PView.USE_FOR_SCALE_WIDTH);
}
}
}
纵向模式下的屏幕截图(正常工作):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/top"
android:layout_width="100dp"
android:layout_height="24dp"
android:layout_above="@+id/middle"
android:layout_centerHorizontal="true"
android:background="#FFFF0000" />
<test.com.heighttest.PView
android:id="@+id/middle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:background="#FFFF00FF" />
<FrameLayout
android:id="@+id/bottom"
android:layout_width="100dp"
android:layout_height="24dp"
android:layout_below="@+id/middle"
android:layout_centerHorizontal="true"
android:background="#FFFFFF00" />
</RelativeLayout>
横向模式下的屏幕截图(工作不正确,因为中间部分过度绘制了顶部和底部):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/top"
android:layout_width="100dp"
android:layout_height="24dp"
android:layout_above="@+id/middle"
android:layout_centerHorizontal="true"
android:background="#FFFF0000" />
<test.com.heighttest.PView
android:id="@+id/middle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:background="#FFFF00FF" />
<FrameLayout
android:id="@+id/bottom"
android:layout_width="100dp"
android:layout_height="24dp"
android:layout_below="@+id/middle"
android:layout_centerHorizontal="true"
android:background="#FFFFFF00" />
</RelativeLayout>
我想在横向模式下获得的屏幕截图
@nidhi pandya提供的解决方案(包括我的修复程序):
在Relativelayout下面添加LinearLayout,并使用其方向垂直排列内容。使用alignparenttop、alignparentbottom和centerinparent属性。它将在两个方向上为您提供相同的输出
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<FrameLayout
android:id="@+id/top"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:background="#FFFF0000" />
<test.com.heighttest.PView
android:id="@+id/middle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:background="#FFFF00FF" />
<FrameLayout
android:id="@+id/bottom"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"
android:background="#FFFFFF00" />
</LinearLayout>
</RelativeLayout>
在Relativelayout下面添加LinearLayout,并使用其方向垂直排列内容。使用alignparenttop、alignparentbottom和centerinparent属性。它将在两个方向上为您提供相同的输出
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<FrameLayout
android:id="@+id/top"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:background="#FFFF0000" />
<test.com.heighttest.PView
android:id="@+id/middle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:background="#FFFF00FF" />
<FrameLayout
android:id="@+id/bottom"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"
android:background="#FFFFFF00" />
</LinearLayout>
</RelativeLayout>
可能的重复项不重复。因为它具有不同的中间项行为,所以不要创建语义相等的问题。重复的问题已经指出,因为这两个问题都是你提出的相同问题。请理解布局系统,但不要为每一个你想要的小变化问问题。据我所知,您只需要相对于另一个视图的位置,再加上该视图的一半大小。检查addRule
功能以定位相对位置views@Bonatti,如果你检查我的问题,你会发现它是不同的问题。如果我使用的合成示例在视觉上与旧示例相似,这并不意味着问题是相同的。不管怎样,我得到了答案可能是重复的,不是重复的。因为它具有不同的中间项行为,所以不要创建语义相等的问题。重复的问题已经指出,因为这两个问题都是你提出的相同问题。请理解布局系统,但不要为每一个你想要的小变化问问题。据我所知,您只需要相对于另一个视图的位置,再加上该视图的一半大小。检查addRule
功能以定位相对位置views@Bonatti,如果你检查我的问题,你会发现它是不同的问题。如果我使用的合成示例在视觉上与旧示例相似,这并不意味着问题是相同的。不管怎样,我得到的答案是,我改变了你们的布局尺寸,我发现它不能完全正确地工作。此外,alignparenttop、alignparentbottom和centerinparent属性在linearlayout中不起作用。但无论如何,谢谢你给了我解决问题的正确方法。我发布在右上角的解决方案很高兴能帮助您。@Anton111111hm我将您的布局大小更改为,我发现它不能完全正确工作。此外,alignparenttop、alignparentbottom和centerinparent属性在linearlayout中不起作用。但无论如何,谢谢你给了我解决问题的正确方法。我在右上角发布了解决方案很高兴能帮助您..@Anton111111