2016-12-05 54 views
1

我创建了一个新的小例子来解释我的新问题。在这个例子中,我创建了一个填充整个屏幕的RelativeLayout。和3个孩子:如何填充布局中的剩余空间而不透支其他元素?

  1. 自定义视图(PVIEW)创建,绘制,必须由宽度在肖像模式和自由空间的高度在横向模式填补所有可用空间,而不覆盖在上面,FrameLayout里的底部(粉红色)比例视图
  2. 的FrameLayout id为顶部必须表现出中等以上(红色)
  3. 的FrameLayout id为底部必须出现如下图所示中间(黄色)

但现在在横向模式下中间项透支顶部和底部。有没有人idieas如何解决它?

布局:

<?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> 

PView.java

package test.com.heighttest; 

import android.content.Context; 
import android.util.AttributeSet; 
import android.widget.RelativeLayout; 


/** 
* Created by Anton Potekhin ([email protected]) 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); 
    } 
} 

MainActivity.java

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); 
     } 
    } 
} 

截图在肖像模式(正常工作):在横向模式

enter image description here

截图(工作不正确,因为中间透支顶部和底部):

enter image description here

截图我想要进入横向模式

enter image description here


解决方案通过@尼迪-潘迪亚(与我的修正):

<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="100dp" 
      android:layout_height="24dp" 
      android:background="#FFFF0000" /> 


     <test.com.heighttest.PView 
      android:id="@+id/middle" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="#FFFF00FF" /> 


     <FrameLayout 
      android:id="@+id/bottom" 
      android:layout_width="100dp" 
      android:layout_height="24dp" 
      android:background="#FFFFFF00" /> 

    </LinearLayout> 
</RelativeLayout> 
+0

的可能的复制[如何填写父相对布局中的所有空间?(http://stackoverflow.com/questions/40935591/how-to-fill-all-space-in-parent-relative-布局) – AlphaQ

+0

它不重复。因为它具有不同的中间项目行为 – Anton111111

+0

不要创建语义上相同的问题。重复已被指出,因为两者都是来自你的同样的问题。请理解布局系统,但不要问你想要的每一个小改动的问题。据我所知,你只需要一个相对于另一个视图的位置,再加上该视图的一半大小。检查'addRule'函数来定位相对视图 – Bonatti

回答

0

添加的LinearLayout下面的RelativeLayout,并使用其定位到您的内容垂直排列。使用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> 
+0

嗯我改变你的布局大小,我看到它的作品不完全正确。此外,alignparenttop,alignparentbottom和centerinparent属性在linearlayout内不起作用。但无论如何,谢谢你给我正确的方式来解决我的问题。我张贴在右上角的解决方案 – Anton111111

+0

很高兴帮助你.. @ Anton111111 –