如何以编程方式调整 ImageView 的大小以匹配 CardView 中的父级
How to resize an ImageView Programmatically to match parent in CardView
我想以编程方式将图像放入现有 imageView 中。
这是 xml 文件,其中我有 imageView 和 TextView:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="10dp"
card_view:cardCornerRadius="4dp">
<!--android:background="?android:selectableItemBackground" nam daje click efekt-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/slika_id"
android:layout_width="match_parent"
android:layout_height="150dp"
android:src="@drawable/bird_corella"
android:layout_centerHorizontal="true"
android:scaleType="fitXY"/>
<TextView
android:id="@+id/zivotinjaIme_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/slika_id"
android:gravity="center_horizontal"
android:textStyle="bold"
android:text="Test"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:background="@android:color/holo_blue_bright"
android:textColor="@android:color/black"
android:textSize="18sp"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
这是来自 main_activity 的代码,我想如何更改它但它不起作用:
ImageView imageView = (ImageView) findViewById(R.id.slika_id);
Display display = getWindowManager().getDefaultDisplay();
int height =((display.getHeight()*20)/100); //30% of the screan
CardView.LayoutParams parms = new CardView.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,height);
imageView.setLayoutParams(parms);
当我在没有 cardView 的情况下使用此代码时,它与 little eddit 一起工作:
这是 xml 只有 imageView 和一个 LinearLayout 的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.imagesize.MainActivity">
<ImageView
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="150dp"
android:id="@+id/slikazivotinje"
android:src="@drawable/bird_corella"
android:scaleType="fitXY"
/>
</LinearLayout>
这是activity_main.java,我在这里成功更改了编程图像大小:
ImageView imageView = (ImageView) findViewById(R.id.slikazivotinje);
Display display = getWindowManager().getDefaultDisplay();
int height =((display.getHeight()*20)/100); //30% of the screan
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,height);
imageView.setLayoutParams(parms);
ImageView imageView = (ImageView) findViewById(R.id.slikazivotinje);
Display display = getWindowManager().getDefaultDisplay();
int height =((display.getHeight()*20)/100); //30% of the screan
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,height);
imageView.setLayoutParams(parms);
我认为问题出在父层上,在 CardView 中我有
<android.support.v7.widget.CardView>
<RelativeLayout>
<ImageView />
<TextView/>
</RelativeLayout>
</android.support.v7.widget.CardView>
Anyone knows how to work this out?
这里是logCat:
FATAL EXCEPTION: main
Process: com.example.android.imagesize, PID: 23919
java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
at android.widget.RelativeLayout$DependencyGraph.findRoots(RelativeLayout.java:1755)
at android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1700)
at android.widget.RelativeLayout.sortChildren(RelativeLayout.java:381)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:389)
at android.view.View.measure(View.java:18796)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.CardView.onMeasure(CardView.java:209)
at android.view.View.measure(View.java:18796)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:18796)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
at android.view.View.measure(View.java:18796)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18796)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1458)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:18796)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18796)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2108)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1224)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1460)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1115)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6023)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
06-02 17:26:46.363 23919-23919/? I/Process: Sending signal. PID: 23919 SIG: 9
用Glide(最好)或者Picasso,更简单,更快,有缓存。所有你需要的
您的 imageView 托管在 RelativeLayout 中,而不是 Cardview 中。改变这个
CardView.LayoutParams parms = new CardView.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,height);
imageView.setLayoutParams(parms);
至此
ViewGroup.LayoutParams params = imageView.getLayoutParams();
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
params.height = height;
imageView.setLayoutParams(params);
我想以编程方式将图像放入现有 imageView 中。 这是 xml 文件,其中我有 imageView 和 TextView:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="10dp"
card_view:cardCornerRadius="4dp">
<!--android:background="?android:selectableItemBackground" nam daje click efekt-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/slika_id"
android:layout_width="match_parent"
android:layout_height="150dp"
android:src="@drawable/bird_corella"
android:layout_centerHorizontal="true"
android:scaleType="fitXY"/>
<TextView
android:id="@+id/zivotinjaIme_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/slika_id"
android:gravity="center_horizontal"
android:textStyle="bold"
android:text="Test"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:background="@android:color/holo_blue_bright"
android:textColor="@android:color/black"
android:textSize="18sp"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
这是来自 main_activity 的代码,我想如何更改它但它不起作用:
ImageView imageView = (ImageView) findViewById(R.id.slika_id);
Display display = getWindowManager().getDefaultDisplay();
int height =((display.getHeight()*20)/100); //30% of the screan
CardView.LayoutParams parms = new CardView.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,height);
imageView.setLayoutParams(parms);
当我在没有 cardView 的情况下使用此代码时,它与 little eddit 一起工作: 这是 xml 只有 imageView 和一个 LinearLayout 的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.imagesize.MainActivity">
<ImageView
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="150dp"
android:id="@+id/slikazivotinje"
android:src="@drawable/bird_corella"
android:scaleType="fitXY"
/>
</LinearLayout>
这是activity_main.java,我在这里成功更改了编程图像大小:
ImageView imageView = (ImageView) findViewById(R.id.slikazivotinje);
Display display = getWindowManager().getDefaultDisplay();
int height =((display.getHeight()*20)/100); //30% of the screan
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,height);
imageView.setLayoutParams(parms);
ImageView imageView = (ImageView) findViewById(R.id.slikazivotinje);
Display display = getWindowManager().getDefaultDisplay();
int height =((display.getHeight()*20)/100); //30% of the screan
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,height);
imageView.setLayoutParams(parms);
我认为问题出在父层上,在 CardView 中我有
<android.support.v7.widget.CardView>
<RelativeLayout>
<ImageView />
<TextView/>
</RelativeLayout>
</android.support.v7.widget.CardView>
Anyone knows how to work this out?
这里是logCat:
FATAL EXCEPTION: main
Process: com.example.android.imagesize, PID: 23919
java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
at android.widget.RelativeLayout$DependencyGraph.findRoots(RelativeLayout.java:1755)
at android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1700)
at android.widget.RelativeLayout.sortChildren(RelativeLayout.java:381)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:389)
at android.view.View.measure(View.java:18796)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.CardView.onMeasure(CardView.java:209)
at android.view.View.measure(View.java:18796)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:18796)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
at android.view.View.measure(View.java:18796)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18796)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1458)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:18796)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18796)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2108)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1224)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1460)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1115)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6023)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
06-02 17:26:46.363 23919-23919/? I/Process: Sending signal. PID: 23919 SIG: 9
用Glide(最好)或者Picasso,更简单,更快,有缓存。所有你需要的
您的 imageView 托管在 RelativeLayout 中,而不是 Cardview 中。改变这个
CardView.LayoutParams parms = new CardView.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,height);
imageView.setLayoutParams(parms);
至此
ViewGroup.LayoutParams params = imageView.getLayoutParams();
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
params.height = height;
imageView.setLayoutParams(params);