0

这让我彻底疯了,基本上,我有一个带有 ImageView 和 LinearLayout 的 RelativeLayout。我希望 LinearLayout 是 ImageView 的宽度,高度是一个常数值并位于 ImageView 的底部(与底部重叠)。

实际发生了什么(我相信,我可能是错的):由于某种原因,在第一次通过时,ImageView 尺寸是原始图像的尺寸(或者我相信),它正在调整 RelativeLayout 的大小,然后当 ImageView 调整大小以适应边界它不会更新父 RelativeLayout 或子级。

我已经覆盖了每一个视图,尝试以各种方式调用它们,但仍然没有运气。

这是视图:

<?xml version="1.0" encoding="utf-8"?>
<uk.co.testing.prototype.prototype.views.RelativeLayoutExtended
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FFF"
    android:paddingLeft="5dip">

  <uk.co.testing.prototype.prototype.views.FixedImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:adjustViewBounds="true" />

  <uk.co.testing.prototype.prototype.views.LinearLayoutExtended 
        android:id="@+id/width_wrapper"
        android:background="@drawable/image_gradient"
        android:layout_width="fill_parent"
        android:layout_height="30dip"
        android:layout_alignParentBottom="true"
        android:orientation="vertical">

  </uk.co.testing.prototype.prototype.views.LinearLayoutExtended>

</uk.co.testing.prototype.views.RelativeLayoutExtended>

这是我的 RelativeLayoutExtended :

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

public class RelativeLayoutExtended extends RelativeLayout
{
    public RelativeLayoutExtended(Context context)
    {
        super(context);
    }

    public RelativeLayoutExtended(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public RelativeLayoutExtended(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }

    @Override 
    public void onSizeChanged( int w, int h, int oldw, int oldh )
    {
        //int heightDensity = ( int ) ( 50 * ( getResources().getDisplayMetrics().density + 0.5f ) );
        //LinearLayoutExtended linearLayout = ( LinearLayoutExtended ) getChildAt( 1 );
        //linearLayout.setLayoutParams( new RelativeLayout.LayoutParams( getWidth(), getHeight() ) );

        //ImageView imageView  = ( ImageView ) getChildAt( 0 );

        //Log.e( "width", "" + imageView.getWidth() );
        //Log.e( "height", "" + imageView.getHeight() );

        super.onSizeChanged( w, h, oldw, oldh );
    }

}

这是我的 LinearLayoutExtended :

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class LinearLayoutExtended extends LinearLayout
{
    public LinearLayoutExtended( Context context )
    {
        super( context );
    }

    public LinearLayoutExtended( Context context, AttributeSet attrs )
    {
        super( context, attrs );
    }

    @Override
    public void setLayoutParams( ViewGroup.LayoutParams params )
    {
        super.setLayoutParams( params );
/*

        Log.e( "set", "width " + params.width );
        Log.e( "set", "height " + params.height );
        // WHY U NO RESIZE?
        post( new Runnable()
        {
            @Override
            public void run()
            {
                requestLayout();
                Log.e( "get", "width " + getWidth() );
                Log.e( "get", "height " + getHeight() );
            }
        } );*/
    }


    @Override 
    public void onSizeChanged( int w, int h, int oldw, int oldh )
    {
        super.onSizeChanged( w, h, oldw, oldh );
    }
}

这是我的 FixedImageView :

import android.R.color;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class FixedImageView extends ImageView
{

    public FixedImageView(Context context )
    {
        super(context );
    }

    public FixedImageView(Context context, AttributeSet attrs )
    {
        super( context, attrs );
    }

    public FixedImageView(Context context, AttributeSet attrs, int defStyle)
    {
        super( context, attrs, defStyle );
    }

    @Override
    public void requestLayout()
    {
        super.requestLayout();
    }

    private RelativeLayout relativeLayout = null;
    private LinearLayout linearLayout = null;

    @Override 
    public void onSizeChanged( int w, int h, int oldw, int oldh )
    {
        //Log.e( "onSizeChanged", "width:" + w ); // 660
        //Log.e( "onSizeChanged", "height:" + h ); // 550

        //int heightDensity = ( int ) ( 50 * ( getResources().getDisplayMetrics().density + 0.5f ) );
        //LinearLayoutExtended linearLayout = ( LinearLayoutExtended ) getChildAt( 1 );
        //linearLayout.setLayoutParams( new RelativeLayout.LayoutParams( getWidth(), getHeight() ) );

        //ImageView imageView  = ( ImageView ) getChildAt( 0 );

        //Log.e( "width", "" + imageView.getWidth() );
        //Log.e( "height", "" + imageView.getHeight() );

        super.onSizeChanged( w, h, oldw, oldh );

        //RelativeLayout relativeLayout = ( RelativeLayout )this.getParent();
        //LinearLayout linearLayout = ( LinearLayout )relativeLayout.getChildAt( 1 );
        //linearLayout.invalidate();

        relativeLayout = ( RelativeLayout )this.getParent();


        linearLayout = ( LinearLayout )relativeLayout.getChildAt( 1 );

        linearLayout.post( new Runnable()
        {
            @Override
            public void run()
            {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams( getWidth(), getHeight() );
                layoutParams.addRule( RelativeLayout.ALIGN_PARENT_BOTTOM );

                linearLayout.setLayoutParams( layoutParams );
                linearLayout.invalidate();
                linearLayout.requestLayout();
            }
        } );

        relativeLayout.post( new Runnable()
        {
            @Override
            public void run()
            {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams( getWidth(), getHeight() );

                relativeLayout.setLayoutParams( layoutParams );
                relativeLayout.invalidate();
                relativeLayout.requestLayout();
            }
        } );

        /*
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT );
        layoutParams.addRule( RelativeLayout.ALIGN_PARENT_BOTTOM );

        RelativeLayout relativeLayout = ( RelativeLayout )this.getParent();

        LinearLayoutExtended linearLayoutExtended = new LinearLayoutExtended( this.getContext() );
        linearLayoutExtended.setBackgroundColor( color.black );
        //linearLayoutExtended.setBackgroundDrawable( getResources().getDrawable( R.drawable.image_gradient ) );
        linearLayoutExtended.setLayoutParams( layoutParams );
        linearLayoutExtended.setOrientation( LinearLayout.VERTICAL );

        relativeLayout.addView( linearLayoutExtended );

        Log.e( "width", "" + linearLayoutExtended.getWidth() ); // 0
        Log.e( "height", "" + linearLayoutExtended.getHeight() ); // 0
        */
    }

}

如您所见,我已经尝试了大量的重新调整大小(通过实验尝试使 LinearLayoutExtended 和 RelativeLayoutExtended 与 FixedImageView 的大小相同),但没有运气。:(

任何建议都会很棒!

谢谢

编辑 :

正在发生的事情的粗略示例:

在此处输入图像描述

4

2 回答 2

1

如果您希望线性布局成为ImageViewdo 的宽度:

android:layout_alignLeft=imageviewid
android:layout_alignRight=imageviewid

LinearLayout

于 2013-07-14T19:54:47.217 回答
0

ImageView您的布局代码设置to的宽度wrap_content并且不提供定位信息。因此,它将位于左上角,RelativeLayout并且仅与内容一样宽。

如果你想要一个RelativeLayout有两个Views 的,它们都是全宽的,那么它们都应该像@Gabe Sechan 建议的那样绑在左右边缘,RelativeLayout或者对它们都这样做:

android:android:layout_alignParentLeft  = "true"
android:android:layout_alignParentRight = "true"

为确保LinearLayout它正好低于ImageView,您还应该设置:

android:layout_below="id/image"
于 2013-07-15T14:09:29.317 回答