How to create relativelayouts with java code?
-
09-12-2019 - |
Frage
I need to learn creating relativelayouts with java code instead of XML code.
for example, will be nice to understand how to build this XML with java code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/btnButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"/>
<Button
android:id="@+id/btnButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
android:layout_toRightOf="@+id/btnButton1"/>
<Button
android:id="@+id/btnButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3"
android:layout_below="@+id/btnButton1"/>
</RelativeLayout>
I can't find any examples of doing this with java code
Please, can someone help me to transform that layout into java code? or can someone give me tutorial links to do relativelayouts with java code?
thanks
Lösung
there are plenty of examples out there! Try this tutorial, it covers all elements that you need... And best, it gives programmatically examples for all kind of layouts, so it will solve future questions, too ;)
http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-relative-layouts/
Andere Tipps
I hope this code help you to implement a relativelayout programmatically
RelativeLayout.LayoutParams expandLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
expandLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
expandLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
expandLayoutParams.topMargin=7;
expandLayoutParams.rightMargin=7;
expandButton.setLayoutParams(expandLayoutParams);
One more thing. The Code that you put i dont think represent the interface that you want to implement. TO make that you want i think is better use linearlayout with orientation equal to horizontal and all the button with layout_width = 0dip and layout_weight = 1
//you create relative layout dynamically like below:
public class TwoPicksOnEachOther extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Grabbing the Application context
final Context context = getApplication();
RelativeLayout relativeLayout = new RelativeLayout(this);
final ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.fish2);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
relativeLayout.addView(iv,lp);
// Creating transparent image
final ImageView iv2 = new ImageView(this);
iv.setImageResource(R.drawable.ctdeasytwo);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
relativeLayout.addView(iv2,lp2);
setContentView(relativeLayout);
}
}