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="#ff659dff" android:layout_alignParentLeft="true" /> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Put At Button Right" 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). RIGHT_OF Rule that aligns a child's left edge with another child's right edge. ALIGN_BASELINE Rule that aligns a child's baseline with another child's baseline. */ // To remove a rule, make it value to zero lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0); lp.addRule(RelativeLayout.RIGHT_OF, btn.getId()); lp.addRule(RelativeLayout.ALIGN_BASELINE, 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 place a view above of another view in a RelativeLayout
- Programmatically place a view left of another view in a RelativeLayout
- Programmatically remove rule from a View in a RelativeLayout
- How to place a view above of another view in a RelativeLayout
- How to place a view at the center of a RelativeLayout
- How to align a view to the vertical center of a RelativeLayout
- How to align a view to the horizontal center of a RelativeLayout
- Align baseline of one view to the baseline of another view in a RelativeLayout
- Align bottom of one view to the bottom of another view in a RelativeLayout
- Align top of one view to the top of another view in a RelativeLayout
Komentar
Posting Komentar