从另一个视图更改视图的尺寸

时间:2013-07-14 19:37:42

标签: java android imageview

这让我完全疯了,基本上,我有一个带有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的大小相同),但没有运气。 :(

任何建议都会很棒!

由于

编辑:

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

enter image description here

2 个答案:

答案 0 :(得分:1)

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

android:layout_alignLeft=imageviewid
android:layout_alignRight=imageviewid
<{1>}上的

答案 1 :(得分:0)

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

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

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

要确保LinearLayout完全低于ImageView,您还应该设置:

android:layout_below="id/image"