gpt4 book ai didi

java - 从另一个 View 更改 View 的尺寸

转载 作者:行者123 更新时间:2023-12-01 14:20:23 25 4
gpt4 key购买 nike

这让我完全抓狂,基本上,我有一个带有 ImageView 和 LinearLayout 的相对布局。我希望 LinearLayout 为 ImageView 的宽度,高度为恒定值并位于 ImageView 的底部(与底部重叠)。

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

我已经覆盖了每一个 View ,尝试以各种方式调用它们,但仍然没有成功。

这是 View :

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

这是我的相对布局扩展:

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

这是我的固定 ImageView :

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 和relativelayoutextend 与固定 ImageView 的大小相同),但没有成功。 :(

任何建议都会很棒!

谢谢

编辑:

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

enter image description here

最佳答案

如果您希望线性布局为ImageView的宽度,请执行以下操作:

android:layout_alignLeft=imageviewid
android:layout_alignRight=imageviewid

LinearLayout上。

关于java - 从另一个 View 更改 View 的尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17642986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com