Button verschwindet in LinearLayout

  • 3 Antworten
  • Letztes Antwortdatum
J

Jajobe

Erfahrenes Mitglied
14
Guten Tag zusammen
Ich habe ein Problem bei meinem kleinen Spiel:
Und zwar habe ich ein leeres Feld, indem ein Button ist. Wenn man auf den Button drückt, springt er auf eine andere Stelle. Nur ist mein Problem, dass vor allem bei Handys mit kleinen Bildschirm der Button häufig verschwindet.
Ich hoffe ihr könnt mir helfen.

Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/blau"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.bjcreative.kleinesspiel.MainActivity" >
   
   

    <ProgressBar
        android:id="@+id/progressBar1"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="wrap_content"
        android:layout_height="70dp"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:minHeight="50dp"
        android:minWidth="200dp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="TextView"
        android:textSize="40sp" />
   

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
       
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginTop="50dp"
            android:text="@string/drueckmich" />
       
<Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/back" />
       
    </LinearLayout>


</LinearLayout>

Code:
package com.bjcreative.buttondrueck;

import java.util.Random;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class GameActivity extends Activity implements OnClickListener {

   
    ProgressBar bar;
    Button btn, cbtn;
    TextView tv;
    int i = 1;
   
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_game);
       
       
       
        tv  = (TextView)findViewById(R.id.textView1);



        final MyCounter timer = new MyCounter(10000,1000);
       
      
           
        
                timer.start();
           
      
      
    }
    public class MyCounter extends CountDownTimer{
       
        public MyCounter(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }
        @Override
        public void onFinish() {
            getString(R.string.trink,R.id.end);
            tv.setText((getString(R.string.trink))+ " " + (100 -  i / 4) +(getString(R.string.end)));
            btn.setOnClickListener(null);
            btn.setVisibility(View.INVISIBLE);
           
           
           
           
           
                  }
        @Override
        public void onTick(long millisUntilFinished) {
            tv.setText((millisUntilFinished/1000)+"");
            System.out.println("Timer  : " + (millisUntilFinished/1000));
        }
       
       
       
   
       
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.

       

        bar = (ProgressBar)findViewById(R.id.progressBar1);
        bar.setMax(200);
       
        btn = (Button)findViewById(R.id.button1);
        btn.setOnClickListener(this);
       
        cbtn = (Button)findViewById(R.id.button2);
        cbtn.setOnClickListener(this);
       
        cbtn.setOnClickListener(new OnClickListener(){
            public void onClick (View v) {
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(intent);
            }
        });
       
       
        cbtn.setVisibility(View.INVISIBLE);
        cbtn.postDelayed(new Runnable() {
                public void run() {
                    cbtn.setVisibility(View.VISIBLE);
                }
            }, 11000); 
       
        return true;
    }
   
    public void onClick(View e){
       
        switch (e.getId()) {
        case (R.id.button1): {
                Random r = new Random();
                int x = r.nextInt(480);
                int y = r.nextInt(800);

                btn.setX(x);  
                btn.setY(y);}
       
       
        if(e == btn){
            i += 4;
           
            if(i >= 200){
                AlertDialog ad = new AlertDialog.Builder(this).create();
                ad.setMessage("Du hast die Leiste voll gekriegt");
                ad.show();
               
                i = 0;
            }
           
            bar.setProgress(i);
        }}}
   
       
       
        }
 
weil du ganz einfach mal die nöchstenx und y koordinaten in pixel angibst und das random aus 2 bestimmten zahlen...alles unter 480x800 px wird probleme kriegen alles darüber wird es komisch aussehen. berechne die nöchste position lieber anhand der dimensionen des bildschirm bzw. der view
 
Ja das habe ich mir auch schon überlegt. Nur weiß ich nicht ganz, wie ich des anstellen kann, das die maximale Position durch Linear View limitiert wird.
 
eine view hat methoden deren dimensionen und positionen zu erfragen
 
Zurück
Oben Unten