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" > <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Put It Center Vertical" /> </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; 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); simpulan 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) btn.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). CENTER_VERTICAL Rule that centers the child vertically with respect to the bounds of its RelativeLayout parent. */ lp.addRule(RelativeLayout.CENTER_VERTICAL); /* 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. */ btn.setLayoutParams(lp); } }); } }


- Programmatically align a view to the center of a RelativeLayout
- Programmatically align a view to the horizontal center of a RelativeLayout
- Programmatically align a view to the right of a RelativeLayout
- Programmatically remove rule from a View in a RelativeLayout
- How to place buttons side by side in a RelativeLayout
- How to place a view below of another 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 right of a RelativeLayout
- How to align a view to the bottom of a RelativeLayout
Komentar
Posting Komentar