これは私を完全に狂わせています。基本的には、内部に ImageView と LinearLayout を持つ RelativeLayout があります。LinearLayout を ImageView の幅にし、高さを定数値にし、ImageView の下部に配置する (下部を重ねる) ようにします。
実際に何が起こっているか(私は間違っている可能性があると思います): 何らかの理由で、最初のパスで ImageView の寸法は、RelativeLayout のサイズを変更している元の画像の寸法です (またはそう信じています)。親の 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 と同じサイズにしようとしました) が、うまくいきませんでした。:(
どんな提案も素晴らしいでしょう!
ありがとう
編集 :
何が起こっているかの粗い例: