簡體   English   中英

在Android Studio中使用Java將按鈕放置到RIGHT_OF另一個小部件

[英]Use java in Android Studio to place button to RIGHT_OF another widget

我正在擴展Fragment的類中在Android Studio中動態創建按鈕。 我已經創建了按鈕(btn),並且要在該按鈕下輸入一個文本(appName),但是現在我想將該按鈕放置在已經存在的另一個按鈕旁邊(我們將其稱為btn_2)。 我很難找出如何使用Java(而不是xml)將一個小部件放置在另一個小部件的旁邊。 這是我到目前為止的內容:

    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);
 }

我遇到錯誤的地方是:

params.addRule(RelativeLayout.RIGHT_OF);

我如何通過希望將新按鈕(btn)放在旁邊的EXISTING按鈕(btn_2)的ID?

然后,我想填補這個空白

// place text under newly created button

如何將新創建的textview(appName)放置在新創建的btn和現有btn_2下。 (也許在我弄清楚如何將一個按鈕放置在另一個按鈕之后,這會更加直接。)

這是我的MainActivity。 我傳遞給HomeFragment createButton方法的圖像和字符串將從另一個設備通過套接字發送給MainActivity:

// 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();
 }

 }

這是我的HomeFragment xml:

<?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>

您必須指定視圖ID才能使視圖right of 嘗試-

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

params.addRule(RelativeLayout.RIGHT_OF, id);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM