JavascriptProva

venerdì 9 settembre 2016

Aggiunta di elementi al mio layout immagine di J-Communicator, con problemi di formato

Ho fatto un po' di progressi nel gestire il caricamento di cartelle.
Questo è il codice:
do{
            boolean isCategoria=false;
            if(crs.getString(4).equals("categoria")) isCategoria=true;

            if(isCategoria==false) layout=new NormalLayout(this);
            else layout=new CategoryLayout(this);

            RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.topMargin=crs.getInt(6);
            params.leftMargin=crs.getInt(5);
            params.width=150;
            params.height=150;
            layout.setLayoutParams(params);


            //CODICE PER L'AGGIUNTA DELL'ETICHETTA:
            TextView etichView=new TextView(this);
            etichView.setText("ETICHETTA");
            layout.addView(etichView);
            RelativeLayout.LayoutParams etichParams=(RelativeLayout.LayoutParams)etichView.getLayoutParams();
            etichParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
            etichParams.topMargin=(int)(layout.getLayoutParams().height*Parametri.percentuale_altezza_linguetta);
            etichView.setTypeface(null, Typeface.BOLD);
            System.out.println(layout.getLayoutParams().height);
            etichView.setLayoutParams(etichParams);



            mainLayout.addView(layout);
        } while(crs.moveToNext());
Ho imparato anche il metodo addRule di LayoutParams.

Ho impostato un modulo Parametri per tenere sotto controllo le misure della cartella in un unico punto senza dover andare a spulciare in mezzo al codice.

L'aggiunta dell'immagine si rivela problematica.
Come fare ad accodare l'immagine all'etichetta?
Come fare se l'etichetta deve essere più lunga a causa di un testo più lungo?
Il problema non è facile affatto... Come gestire, in sostanza, lo spazio?
Trovo un attributo in XML. Potrà servire?

Allo stesso modo, l'immagine andrà costretta in uno spazio limitato.
Mi trovo piuttosto in difficoltà...
Mi isolo un esperimento sul mio tavolo da lavoro.
public class MainActivity extends AppCompatActivity {

    RelativeLayout mainLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mainLayout=(RelativeLayout)findViewById(R.id.mainLayout);
        TextView textView=new TextView(this);
        textView.setText("PRIMA");
        mainLayout.addView(textView);


        TextView textView2=new TextView(this);
        textView2.setText("SECONDA");
        mainLayout.addView(textView2);
    }
}
Vediamola...

Ecco:



Le due TextView vengono sovrapposte.


Ora voglio trovare il modo di porle una sotto l'altra.
Forse sarà meglio isolarle dallo sfondo colorandole...

        TextView textView=new TextView(this);
        textView.setText("PRIMA");
        textView.setBackgroundColor(Color.YELLOW);
        mainLayout.addView(textView);


        TextView textView2=new TextView(this);
        textView2.setText("SECONDA");
        textView2.setBackgroundColor(Color.GREEN);
        mainLayout.addView(textView2);
Così facendo, si vede soltanto quella apposta per ultima.
Ora cominciamo a cercare di far qualcosa coi parametri.

        TextView textView=new TextView(this);
        textView.setText("PRIMA");
        textView.setBackgroundColor(Color.YELLOW);
        mainLayout.addView(textView);
        


        TextView textView2=new TextView(this);
        textView2.setText("SECONDA");
        textView2.setBackgroundColor(Color.GREEN);
        mainLayout.addView(textView2);
        RelativeLayout.LayoutParams params=(RelativeLayout.LayoutParams)textView2.getLayoutParams();
        params.addRule(RelativeLayout.BELOW,textView.getId());
Vediamo se funziona ponendomi textView2 sotto textView.

Non funziona.
Invece questo codice...
        TextView textView2=new TextView(this);
        textView2.setText("SECONDA");
        textView2.setBackgroundColor(Color.GREEN);
        mainLayout.addView(textView2);
        RelativeLayout.LayoutParams params=(RelativeLayout.LayoutParams)textView2.getLayoutParams();
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
funziona, mettendo la seconda textView all'estremità inferiore del layout.

Ho trovato la soluzione:
        TextView textView=new TextView(this);
        textView.setText("PRIMA");
        textView.setBackgroundColor(Color.YELLOW);
        mainLayout.addView(textView);
        textView.setId(View.generateViewId());



        TextView textView2=new TextView(this);
        textView2.setText("SECONDA");
        textView2.setBackgroundColor(Color.GREEN);
        mainLayout.addView(textView2);
        RelativeLayout.LayoutParams params=(RelativeLayout.LayoutParams)textView2.getLayoutParams();
        params.addRule(RelativeLayout.BELOW,textView.getId());
In questo modo il BELOW funziona.

Ora cerco di applicarlo al mio J-Communicator.
            //CODICE PER L'AGGIUNTA DELL'ETICHETTA:
            TextView etichView=new TextView(this);
            etichView.setText("ETICHETTA");
            layout.addView(etichView);
            RelativeLayout.LayoutParams etichParams=(RelativeLayout.LayoutParams)etichView.getLayoutParams();
            etichParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
            etichParams.topMargin=(int)(layout.getLayoutParams().height*Parametri.percentuale_altezza_linguetta);
            etichView.setTypeface(null, Typeface.BOLD);
            System.out.println(layout.getLayoutParams().height);
            etichView.setLayoutParams(etichParams);
            etichView.setId(View.generateViewId());

            //CODICE PER L'AGGIUNTA DELL'IMMAGINE
            ImageView imageView=new ImageView(this);
            Bitmap bitmap=Funzioni.StringToBitmap(crs.getString(3));

            layout.addView(imageView);
            RelativeLayout.LayoutParams imgParams=(RelativeLayout.LayoutParams) imageView.getLayoutParams();
            imgParams.addRule(RelativeLayout.BELOW,etichView.getId());
            imageView.setLayoutParams(imgParams);
E vediamo...

            ImageView imageView=new ImageView(this);
            Bitmap bitmap=Funzioni.StringToBitmap(crs.getString(3));
            imageView.setImageBitmap(bitmap);
            layout.addView(imageView);
            RelativeLayout.LayoutParams imgParams=(RelativeLayout.LayoutParams) imageView.getLayoutParams();
            imgParams.addRule(RelativeLayout.BELOW,etichView.getId());
r> Questo funziona.
Ora devo vedere di adattare le misure.

Non è facile.
In OnStart non riesco ad avere le misure della TextView, e nemmeno in OnResume.
Leggo che c'è qualche possibilità intercettando l'evento onWindowFocusChange ma mi chiedo se questo venga prima o dopo onStart e se una variabile modificata in questo evento mostri la sua modifica quindi anche in onStart.
Faccio una prova. Creo una variabile di istanza e la modifico per poi retrievarne il valore in onStart.
String variabileSpia;

.....

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        variabileSpia="PRIMO VALORE";
Ora monto su il metodo:
    @Override
    public void onWindowFocusChanged(boolean hasFocus){
        variabileSpia="SECONDO VALORE";
    }
e vado a leggere in onStart...
    @Override
    protected void onStart(){
        super.onStart();
        System.out.println("VARIABILE SPIA "+variabileSpia);
        mLucchettiChangeListener.onLucchettiChange(boolLucchettoSinistroAperto,boolLucchettoDestroAperto);
        refreshScreen();

    }
Vediamo...

No:
I/System.out: VARIABILE SPIA PRIMO VALORE
Vediamo in onResume...
    @Override
    protected void onResume(){
        super.onResume();
        System.out.println("VARIABILE SPIA "+variabileSpia);
    }
Nemmeno...
I/System.out: VARIABILE SPIA PRIMO VALORE
Bene... non so come fare.

Nessun commento:

Posta un commento