如何显示LinearLayout的内容?

时间:2016-04-25 18:28:39

标签: android imageview android-linearlayout display

我想从今天早上起自己回答这个问题,但我变得疯了。我确定这是一个愚蠢的问题,但我无法找到解决方案。 这是我的代码:

VariablesManagement.area1_pictos=area1;
//area1 become null after some code
Log.d("Number", String.valueOf(area1.getChildCount())); //print 0 and it's normal
area1=VariablesManagement.area1_pictos;
Log.d("Number", String.valueOf(area1.getChildCount())); //print 4 and it's normal

问题是我无法显示我的LinearLayout中包含的imageView。什么都没有印刷品。我不明白,因为我们可以看到里面有ImageViews。

我希望你理解我的问题(对不起,我不是英文),非常感谢你的帮助。

编辑: 这是一个更详细的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.repas);
area1 = (LinearLayout) findViewById(R.id.area1);

if( savedInstanceState != null ) {
    Log.d("Number", String.valueOf(area1.getChildCount()));
    area1=VariablesManagement.area1_pictos;
    Log.d("Number", String.valueOf(area1.getChildCount()));
}

@Override
public void onSaveInstanceState(Bundle outState)
{
VariablesManagement.area1_pictos=null;
VariablesManagement.area1_pictos=area1; 

super.onSaveInstanceState(outState);
}

我完整的xml代码:

<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"
android:orientation="vertical"
android:weightSum="4">


<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="3"
    android:fillViewport="true"
    android:background="#404040" >

    <LinearLayout

                android:id="@+id/area1"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:background="#FF0000"
                android:layout_gravity="center_horizontal"
                android:gravity="center_horizontal">
    </LinearLayout>
</ScrollView>

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="#00ff00"
    android:id="@+id/horizontalScrollView" >

    <LinearLayout
            android:id="@+id/area2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:gravity="center">
    </LinearLayout>

</HorizontalScrollView>

以下是我对那些有动力的人的完整代码:

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.text.method.ScrollingMovementMethod;
import android.content.ClipData;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnDragListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import java.util.ArrayList;

public class Repas extends Activity {

    private LinearLayout area1, area2;
    public static final int HAUTEUR_PICTO =100;
    public static final int LARGEUR_PICTO = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.repas);
        area1 = (LinearLayout) findViewById(R.id.area1);
        area2 = (LinearLayout) findViewById(R.id.area2);

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        final int width_screen=dm.widthPixels;
        area2.setMinimumWidth(width_screen);
        area1.setMinimumWidth(LARGEUR_PICTO);

        FilesManagement.parcourirDossierPicto(this, VariablesManagement.dossier_pictos_timetable);
        if( savedInstanceState != null ) {
            Log.d("Number", String.valueOf(area1.getChildCount()));
            area1=VariablesManagement.area1_pictos;
            Log.d("Number", String.valueOf(area1.getChildCount()));
        }
        else {
            for (int i = 0; i < VariablesManagement.drawable_stockage.size(); i++) {
                ImageView imageView = new ImageView(this);
                imageView.setImageDrawable((Drawable) VariablesManagement.drawable_stockage.get(i));
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                lp.setMargins(5, 5, 5, 5);
                imageView.setLayoutParams(lp);
                imageView.setOnTouchListener(myOnTouchListener);
                imageView.setId(i);
                area2.addView(imageView);
            }
            VariablesManagement.drawable_stockage = null;
            VariablesManagement.drawable_stockage = new ArrayList();
        }

        area1.setOnDragListener(myOnDragListener);
        area2.setOnDragListener(myOnDragListener);
    }

View.OnTouchListener myOnTouchListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {

            int action = event.getAction();
            if (action==MotionEvent.ACTION_DOWN)
            {
                ClipData data = ClipData.newPlainText("", "");
                DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
                v.startDrag(data, shadowBuilder, v, 0);
                //v.setVisibility(View.INVISIBLE);
            }
            return false;
        }
};


    OnDragListener myOnDragListener = new OnDragListener() {

        @Override
        public boolean onDrag(View v, DragEvent event) {

            switch (event.getAction()) {
                case DragEvent.ACTION_DROP:
                    View view = (View)event.getLocalState();
                    LinearLayout oldParent = (LinearLayout)view.getParent();

                    oldParent.removeView(view);
                    LinearLayout newParent = (LinearLayout)v;


                    if(newParent==area1){
                    final int index = calculateNewIndex(event.getY());
                    newParent.addView(view, index);
                    }
                    else{
                        newParent.addView(view);
                    }


                    final ScrollView sv = (ScrollView)findViewById(R.id.scrollView);
                    sv.post(new Runnable() {
                        public void run() {

                            sv.scrollTo(0, sv.getBottom());

                        }
                    });

                    break;
                default:
                    break;
            }
            return true;
        }

    };

    @Override
    public void onSaveInstanceState(Bundle outState)
    {
        VariablesManagement.area1_pictos=null;
        VariablesManagement.area1_pictos=area1;
        VariablesManagement.area2_pictos=null;
        VariablesManagement.area2_pictos=area2;

        super.onSaveInstanceState(outState);
    }


private int calculateNewIndex(float y) {
    int index=(int) Math.floor((y-HAUTEUR_PICTO/2) / HAUTEUR_PICTO);
    Log.d("indexy", String.valueOf((y-HAUTEUR_PICTO/2)));
    Log.d("index", String.valueOf(index) );
    if(index>area1.getChildCount())
    {
        index=area1.getChildCount();
    }
    return index;
}




}

0 个答案:

没有答案