2016-05-02 9 views
0

Ich erstelle eine Schaltfläche dynamisch in Android Studio in einer Klasse, die Fragment erweitert. Ich habe meine Schaltfläche erstellt (btn) und einen Text, den ich unter dieser Schaltfläche (appName) gehen möchte, aber jetzt möchte ich die Schaltfläche neben einer anderen Schaltfläche platzieren, die bereits vorhanden ist (nennen wir es btn_2). Ich habe Probleme herauszufinden, wie man ein Widget nebeneinander mit Java und nicht mit XML platziert. Hier ist, was ich bisher:Verwenden Sie Java in Android Studio, um die Schaltfläche RIGHT_OF eines anderen Widgets zu platzieren

public void createButton (BitmapDrawable bitmapdrawable, String applicationName){ 

    LayoutInflater inflater=null; 
    ViewGroup container = null; 

    // Get Layout home_fragment 
    View rootView = inflater.inflate(R.layout.home_fragment, container, false); 
    RelativeLayout rLayout = (RelativeLayout) rootView.findViewById(R.id.home_fragment); 


    // create image button with background as bitmapdrawable 
    ImageButton btn = new ImageButton(getActivity()); 
    btn.setImageDrawable(bitmapdrawable); 

    // create textview with applicationName 
    TextView appName = new TextView(getActivity()); 
    appName.setText(applicationName); 

    // place new button next to existing button 
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
      RelativeLayout.LayoutParams.WRAP_CONTENT, 
      RelativeLayout.LayoutParams.WRAP_CONTENT); 


    params.addRule(RelativeLayout.RIGHT_OF); 
    btn.setLayoutParams(params); 

    // place text under newly created button 





    // Put button and text in layout 
    rLayout.addView(btn); 
    rLayout.addView(appName); 
} 

Wo ich in Fehler leite ist:

params.addRule(RelativeLayout.RIGHT_OF); 

Wie gehe ich durch die ID der vorhandenen Schaltfläche (btn_2), dass ich mein neues wollen Schaltfläche (btn) neben platziert werden?

Dann mag ich in dieser leeren Lücke füllen

// place text under newly created button 

, wie mein neu erstellen Textview (appName) unter meinem neu geschaffenen BTN und unter meinem vorhandenen btn_2 zu platzieren. (Vielleicht wird das einfacher, nachdem ich herausgefunden habe, wie man einen Knopf neben dem anderen platziert.)

Hier ist meine MainActivity. Mein Bild und Zeichenfolge, die ich vorbei bin zu HomeFragment Create Verfahren werden an MainActivity über eine Socket von einem anderen Gerät gesendet:

// main activity (FragmentActivity provides fragment compatibility pre-HC) 
public class MainActivity extends FragmentActivity implements MenuFragment.OnMenufragListener { 


private Thread repeatTaskThread; 
private byte[] byteArray; 

// called when the activity is first created 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 


    // Listening for last installed app to create button 
    RepeatTask(); 
} 


@Override 
public void onMenufrag(Fragment s) { 

    // get body fragment (native method is getFragmentManager) 
    HomeFragment fragment = (HomeFragment) getSupportFragmentManager().findFragmentById(R.id.home_fragment); 

    // if fragment is not null and in layout, set text, else launch BodyActivity 
    if ((fragment!=null)&&fragment.isInLayout()) { 
    fragment.getView(); 
    } else { 
    Intent intent = new Intent(this,HomeFragment.class); 
    startActivity(intent); 
    } 

} 


private void RepeatTask() 
{ 
    repeatTaskThread = new Thread() 
    { 
    public void run() 
    { 
     while (true) 
     { 


      try { 

       System.out.println("TRY_1"); 

       Socket socket = new Socket("192.168.0.26", 5050); 

       // Get data sent through socket 
       DataInputStream DIS = new DataInputStream(socket.getInputStream()); 

       System.out.println("DataInputStream Started"); 

       // read data that got sent 
       String applicationName = DIS.readUTF(); 


       // read array data for bitmap 


       //Drawable icon = getPackageManager().getApplicationIcon(packagename); 
       //System.out.println("icon" + icon); 



       int len = DIS.readInt(); 
       byte[] data = new byte[len]; 

       DIS.readFully(data, 0, data.length); 



       // Convert data to jpeg first then to bitmap (cant convert byte array directly to bitmap) 
       YuvImage yuvimage=new YuvImage(data, ImageFormat.NV21, 100, 100, null); 
       ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
       yuvimage.compressToJpeg(new Rect(0, 0, 100, 100), 80, baos); 
       byte[] jdata = baos.toByteArray(); 

       // Convert to Bitmap 
       Bitmap bmp = BitmapFactory.decodeByteArray(jdata, 0, jdata.length); 


       // Image to png in file directory 

       // STREAM IMAGE DATA TO FILE 
       // This is how I know I am correctly getting my png image (no errors here) 


       // convert bitmap to drawable 
       Drawable d = new BitmapDrawable(getResources(), bmp); 

       // convert drawable to bitmapdrawable 
       BitmapDrawable bitmapdrawable = (BitmapDrawable)d; 
       Log.d("tag_name", "BITMAP Drawable" + bitmapdrawable); 

       // Create file to stream bitmpadrawable 
       FileOutputStream fosIcon = openFileOutput(applicationName + ".png", Context.MODE_PRIVATE); 

       // compress bitmapdrawable into png and write to file that was just created 
       bitmapdrawable.getBitmap().compress(Bitmap.CompressFormat.PNG, 100, fosIcon); 
       InputStream inputStream = openFileInput(applicationName + ".png"); 

       Bitmap bitmap = BitmapFactory.decodeStream(inputStream); 

       // GET FILE DIRECTORY 

       File imageFile = new File(getFilesDir(), applicationName + ".png"); 


       HomeFragment createbutton = new HomeFragment(); 
       createbutton.createButton(bitmapdrawable, applicationName); 
       Log.d("tag_name", "Entered Home Fragment"); 


       socket.close(); 


      } catch (Exception e) { 


       System.out.println("Exception is "+e.toString()); 

      } 



      try 
      { 
       // Sleep for 5 seconds 
       Thread.sleep(5000); 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 
     } 
    }; 
    }; 
    repeatTaskThread.start(); 
} 

} 

Hier ist meine xml für HomeFragment:

<?xml version="1.0" encoding="utf-8"?> 

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center_horizontal" 
    android:layout_margin="5dp" 
    android:id="@+id/home_fragment" 
    > 


</RelativeLayout> 

</HorizontalScrollView> 

Antwort

0

Sie haben die angeben Ansichts-ID, um Ihre Ansicht zu ändern right of. Versuchen Sie -

TextView appName = new TextView(getActivity()); 
int id = 1001; 
appName.setId(id); 

params.addRule(RelativeLayout.RIGHT_OF, id); 
Verwandte Themen