ImageView没有在android中获取父级的完整高度和宽度

时间:2017-10-02 11:28:55

标签: android android-layout

我正在创建一个简单的拖放应用程序。我有一个{3}和3列的label/1GridLayoutLinearLayout的孩子。现在我将Im​​ageViews放在那些GridLayout中。我想将LinearLayout' s ImageViewwidth设为height或覆盖match_parentheight的完整widthLinearLayout它被删除的public class PuzzleActivity extends AppCompatActivity { @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.puzzle_layout); findViewById(R.id.a0).setOnTouchListener(new MyTouchListener()); findViewById(R.id.b0).setOnTouchListener(new MyTouchListener()); findViewById(R.id.c0).setOnTouchListener(new MyTouchListener()); findViewById(R.id.d0).setOnTouchListener(new MyTouchListener()); findViewById(R.id.e0).setOnTouchListener(new MyTouchListener()); findViewById(R.id.row0col0).setOnDragListener(new MyDragListener()); findViewById(R.id.row0col1).setOnDragListener(new MyDragListener()); findViewById(R.id.row0col2).setOnDragListener(new MyDragListener()); findViewById(R.id.row1col0).setOnDragListener(new MyDragListener()); findViewById(R.id.row1col1).setOnDragListener(new MyDragListener()); findViewById(R.id.row1col2).setOnDragListener(new MyDragListener()); findViewById(R.id.row2col0).setOnDragListener(new MyDragListener()); findViewById(R.id.row2col1).setOnDragListener(new MyDragListener()); findViewById(R.id.row2col2).setOnDragListener(new MyDragListener()); } private final class MyTouchListener implements View.OnTouchListener { public boolean onTouch(View view, MotionEvent motionEvent) { if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { ClipData data = ClipData.newPlainText("", ""); View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder( view); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { view.startDragAndDrop(data, shadowBuilder, view, 0); } else { view.startDrag(data, shadowBuilder, view, 0); } view.setVisibility(View.INVISIBLE); return true; } else { return false; } } } class MyDragListener implements View.OnDragListener { private View.OnClickListener myListener = new View.OnClickListener() { @Override public void onClick(View view) { Log.e("Image name", view.getContentDescription()+""); } }; @Override public boolean onDrag(View v, DragEvent event) { int action = event.getAction(); switch (event.getAction()) { case DragEvent.ACTION_DRAG_STARTED: // do nothing break; case DragEvent.ACTION_DRAG_ENTERED: /** * Change background of the layout where item is entering */ v.setBackgroundColor(Color.parseColor("#ECECEC")); break; case DragEvent.ACTION_DRAG_EXITED: /** * Change background of the layout back to normal once item is moved out of it */ v.setBackground(ContextCompat.getDrawable(PuzzleActivity.this, R.drawable.layout_background)); break; case DragEvent.ACTION_DROP: // Dropped, reassign View to ViewGroup View view = (View) event.getLocalState(); //ViewGroup owner = (ViewGroup) view.getParent(); // Removed //owner.removeView(view); // Removed LinearLayout container = (LinearLayout) v; if (container.getChildCount() > 0) { container.removeAllViews(); } // Added the following to copy the old view's bitmap to a new ImageView: ImageView oldView = (ImageView) view; ImageView newView = new ImageView(getApplicationContext()); newView.setId(oldView.getId()); newView.setScaleType(ImageView.ScaleType.FIT_XY); newView.setAdjustViewBounds(true); newView.setContentDescription(oldView.getContentDescription()); newView.setOnClickListener(myListener); ViewGroup.LayoutParams params = container.getLayoutParams(); newView.setLayoutParams(params); newView.setImageBitmap(((BitmapDrawable) oldView.getDrawable()).getBitmap()); container.addView(newView); // Changed to add new view instead view.setVisibility(View.VISIBLE); break; case DragEvent.ACTION_DRAG_ENDED: v.setBackground(ContextCompat.getDrawable(PuzzleActivity.this, R.drawable.layout_background)); default: break; } return true; } } }

这是我的代码:

ImageView

我正在尝试下面的代码,在width案例中设置height' s DragEvent.ACTION_DROPViewGroup.LayoutParams params = container.getLayoutParams(); newView.setLayoutParams(params); newView.setImageBitmap(((BitmapDrawable) oldView.getDrawable()).getBitmap()); 。但它不起作用:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/kolamShapesLayout"
        android:layout_centerInParent="true"
        app:columnCount="3"
        android:paddingTop="64dp"
        android:paddingBottom="64dp"
        android:paddingEnd="20dp"
        android:paddingStart="20dp"
        app:rowCount="3">

        <LinearLayout
            android:id="@+id/row0col0"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_rowWeight="1"
            android:background="@drawable/layout_background"
            android:orientation="horizontal"
            app:layout_columnWeight="1"
            app:layout_gravity="fill_horizontal">

        </LinearLayout>

        <LinearLayout
            android:id="@+id/row0col1"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_rowWeight="1"
            android:background="@drawable/layout_background"
            android:orientation="horizontal"
            app:layout_columnWeight="1"
            app:layout_gravity="fill_horizontal">

        </LinearLayout>

        <LinearLayout
            android:id="@+id/row0col2"
            android:layout_width="0dp"
            app:layout_columnWeight="1"
            android:layout_height="0dp"
            app:layout_rowWeight="1"
            android:background="@drawable/layout_background"
            android:orientation="horizontal"
            app:layout_gravity="fill_horizontal">

        </LinearLayout>

        <LinearLayout
            android:id="@+id/row1col0"
            android:layout_width="0dp"
            app:layout_columnWeight="1"
            android:layout_height="0dp"
            app:layout_rowWeight="1"
            android:adjustViewBounds="true"
            android:background="@drawable/layout_background"
            android:orientation="horizontal"
            app:layout_gravity="fill_horizontal">

        </LinearLayout>

        <LinearLayout
            android:id="@+id/row1col1"
            android:layout_width="0dp"
            app:layout_columnWeight="1"
            android:layout_height="0dp"
            app:layout_rowWeight="1"
            android:background="@drawable/layout_background"
            android:orientation="horizontal"
            app:layout_gravity="fill_horizontal">

        </LinearLayout>

        <LinearLayout
            android:id="@+id/row1col2"
            android:layout_width="0dp"
            app:layout_columnWeight="1"
            android:layout_height="0dp"
            app:layout_rowWeight="1"
            android:background="@drawable/layout_background"
            android:orientation="horizontal"
            app:layout_gravity="fill_horizontal">

        </LinearLayout>

        <LinearLayout
            android:id="@+id/row2col0"
            android:layout_width="0dp"
            app:layout_columnWeight="1"
            android:layout_height="0dp"
            app:layout_rowWeight="1"
            android:background="@drawable/layout_background"
            android:orientation="horizontal"
            app:layout_gravity="fill_horizontal">

        </LinearLayout>

        <LinearLayout
            android:id="@+id/row2col1"
            android:layout_width="0dp"
            app:layout_columnWeight="1"
            android:layout_height="0dp"
            app:layout_rowWeight="1"
            android:background="@drawable/layout_background"
            android:orientation="horizontal"
            app:layout_gravity="fill_horizontal">

        </LinearLayout>

        <LinearLayout
            android:id="@+id/row2col2"
            android:layout_width="0dp"
            app:layout_columnWeight="1"
            android:layout_height="0dp"
            app:layout_rowWeight="1"
            android:background="@drawable/layout_background"
            android:orientation="horizontal"
            app:layout_gravity="fill_horizontal">

        </LinearLayout>

    </android.support.v7.widget.GridLayout>

    <android.support.v7.widget.GridLayout
        android:id="@+id/kolamShapesLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:columnCount="5"
        app:rowCount="1">

        <ImageView
            android:id="@+id/a0"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:src="@drawable/a0"
            app:layout_columnWeight="1"
            android:contentDescription="@string/a0"
            app:layout_gravity="fill_horizontal" />

        <ImageView
            android:id="@+id/c0"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:contentDescription="@string/c0"
            android:src="@drawable/c0"
            app:layout_columnWeight="1"
            app:layout_gravity="fill_horizontal" />

        <ImageView
            android:id="@+id/e0"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:contentDescription="@string/e0"
            android:src="@drawable/e0"
            app:layout_columnWeight="1"
            app:layout_gravity="fill_horizontal" />

        <ImageView
            android:id="@+id/d0"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:src="@drawable/d0"
            android:contentDescription="@string/d0"
            app:layout_columnWeight="1"
            app:layout_gravity="fill_horizontal" />

        <ImageView
            android:id="@+id/b0"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:contentDescription="@string/b0"
            android:src="@drawable/b0"
            app:layout_columnWeight="1"
            app:layout_gravity="fill_horizontal" />

    </android.support.v7.widget.GridLayout>

</RelativeLayout>

这是我的布局xml:

ImageView

目前ImageView不可见,因为我在布局中删除了jQuery("#testTabs").tabs({ heightStyle: "fill", activate: function (event, ui) { var index = ui.newTab.index(); } });

2 个答案:

答案 0 :(得分:-1)

检查父布局中的填充和边距以及图像布局还检查完全填充父布局的图像大小

答案 1 :(得分:-1)

更改ImageView

的宽度

android:layout_width="0dp"

android:layout_width="match_parent"

使用以下

<ImageView
    android:id="@+id/a0"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    android:src="@drawable/a0"
    app:layout_columnWeight="1"
    android:contentDescription="@string/a0"
    app:layout_gravity="fill_horizontal" />

你很高兴。