如何以编程方式调整ImageView的大小以匹配CardView中的父级

时间:2016-06-02 15:40:10

标签: android android-cardview image-size programmatically-created

我想以编程方式将图像放入现有的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的代码时,它使用的是小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,我成功地改变了programmaticlyly图像大小:

 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

2 个答案:

答案 0 :(得分:1)

您的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);

答案 1 :(得分:0)

使用Glide(最好)或毕加索,它更简单,更快,有缓存。所有你需要的