0

Ich möchte das Bild programmatisch in vorhandene imageView einfügen. Dies ist XML-Datei, wo ich Imageview und Textview haben:So passen Sie ein ImageView programmatisch an, damit es in CardView mit dem übergeordneten Element übereinstimmt

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

Hier ist Code aus main_activity, wie ich es ändern wollen und seine nicht funktioniert:

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

Wenn ich diesen Code verwenden, ohne cardView seine mit wenig eddit arbeiten: Hier ist xml-Code mit nur Imageview, und ein 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> 

Dies ist activity_main.java w hier i programmaticlly Bildgröße erfolgreich ändern:

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

Ich denke Problem ist sowas mit Mutterschichten, in CardView habe ich

<android.support.v7.widget.CardView> 
    <RelativeLayout> 
     <ImageView /> 
     <TextView/> 
    </RelativeLayout> 
</android.support.v7.widget.CardView> 

Anyone knows how to work this out? 

Hier ist 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 
+0

ich es versuchen, aber nicht – beginner

Antwort

1

Ihr ImageView wird in einem RelativeLayout gehostet, nicht in einem Cardview. Ändern Sie diese

CardView.LayoutParams parms = new CardView.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,height); 
    imageView.setLayoutParams(parms); 

dieser

ViewGroup.LayoutParams params = imageView.getLayoutParams(); 
params.width = ViewGroup.LayoutParams.MATCH_PARENT; 
params.height = height; 
imageView.setLayoutParams(params); 
+0

das hat funktioniert, danke – beginner

0

Verwendung Glide (die am besten) oder Picasso, es ist einfacher, schneller, mit Cache. Alles was Sie brauchen

+0

funktioniert, die im Grunde für recyclleview mein Einzelteil ist, und ich möchte Größe dieses Bild Ansicht ändern, wenn i-Taste klicken und dann Später werde ich diese Elemente mit denselben Bildern, aber nur verschiedenen Größen füllen. – beginner

+0

In meiner App habe ich recycleView mit Raster-Layout, und ich möchte clcik-Taste, um es von Raster zu Listview zu ändern. – beginner