私はFragmentを拡張するクラスでAndroid Studioでボタンを動的に作成しています。私は自分のボタン(btn)とそのボタン(appName)の下に行きたいテキストを作成しましたが、今はすでに存在する別のボタンの隣にボタンを配置したい(btn_2と呼ぶことができます)。私は、JavaではなくXMLを使用して別のウィジットを配置する方法を見つけるのが難しいです。私はエラーに実行していますAndroid Studioのjavaを使用して別のウィジェットRIGHT_OFにボタンを配置
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_2)のIDを通過するにはどうすればよい、私は私の新しいをしたいことをここに私はこれまで持っているものですボタンの隣に配置するボタン(btn)?
その後、私はこの空のギャップ私の新しく作成されたBTNの下で、私の既存のbtn_2の下に私の新しく作成されたのTextView(appNameの)を配置する方法の
// place text under newly created button
に記入します。 (たぶん、もう一方のボタンを隣り合わせに置く方法を見つけたら、もっと簡単になるでしょう)
ここは私の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>