activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/rl" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" tools:context=".MainActivity" android:background="@android:color/white" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Sample TextView" android:padding="10dp" android:background="#ffa5c3ff" android:layout_alignParentBottom="true" /> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Put At Button Above" android:layout_centerInParent="true" /> </RelativeLayout>
MainActivity.java
package com.cfsuman.me.androidcodesnippets; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.RelativeLayout.LayoutParams; import android.widget.TextView; public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Get the widgets reference from XML layout RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl); selesai TextView tv = (TextView) findViewById(R.id.tv); selesai Button btn = (Button) findViewById(R.id.btn); // Set a click listener for Button widget btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { /* LayoutParams are used by views to tell their parents how they want to be laid out public ViewGroup.LayoutParams getLayoutParams () Get the LayoutParams associated with this view. All views should have layout parameters. These supply parameters to the parent of this view specifying how it should be arranged. There are many subclasses of ViewGroup.LayoutParams, and these correspond to the different subclasses of ViewGroup that are responsible for arranging their children. */ LayoutParams lp = (LayoutParams) tv.getLayoutParams(); /* addRule(int verb) Adds a layout rule to be interpreted by the RelativeLayout. This method should only be used for constraints that don't refer to another sibling (e.g., CENTER_IN_PARENT) or take a boolean value (TRUE for true or 0 for false). addRule(int verb, int anchor) Adds a layout rule to be interpreted by the RelativeLayout. Use this for verbs that take a target, such as a sibling (ALIGN_RIGHT) or a boolean value (VISIBLE). ABOVE Rule that aligns a child's bottom edge with another child's top edge. ALIGN_LEFT Rule that aligns a child's left edge with another child's left edge. */ // To remove a rule, make it value to zero lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 0); lp.addRule(RelativeLayout.ABOVE, btn.getId()); lp.addRule(RelativeLayout.ALIGN_LEFT, btn.getId()); /* public void setLayoutParams (ViewGroup.LayoutParams params) Set the layout parameters associated with this view. These supply parameters to the parent of this view specifying how it should be arranged. There are many subclasses of ViewGroup.LayoutParams, and these correspond to the different subclasses of ViewGroup that are responsible for arranging their children. */ tv.setLayoutParams(lp); } }); } }


- Programmatically align a view to the center of a RelativeLayout
- Programmatically align a view to the vertical center of a RelativeLayout
- Programmatically align a view to the bottom of a RelativeLayout
- How to align a view to the top of a RelativeLayout
- Programmatically place a view left of another view in a RelativeLayout
- Programmatically place a view right of another view in a RelativeLayout
- Programmatically remove rule from a View in a RelativeLayout
- How to set an image to RelativeLayout background
- How to scale RelativeLayout background image
- How to create a RelativeLayout programmatically
Komentar
Posting Komentar