Example:
package com.AndroidAlignParent;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class AndroidAlignParentActivity extends Activity {
LinearLayout myLayout;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myLayout = (LinearLayout)findViewById(R.id.myLayout);
Button btnAlignTop = (Button)findViewById(R.id.alignTop);
Button btnAlignBottom = (Button)findViewById(R.id.alignBottom);
btnAlignTop.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
RelativeLayout.LayoutParams params
= new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
myLayout.setLayoutParams(params);
Toast.makeText(AndroidAlignParentActivity.this,
"ALIGN_PARENT_TOP",
Toast.LENGTH_LONG).show();
}});
btnAlignBottom.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
RelativeLayout.LayoutParams params
= new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
myLayout.setLayoutParams(params);
Toast.makeText(AndroidAlignParentActivity.this,
"ALIGN_PARENT_BOTTOM",
Toast.LENGTH_LONG).show();
}});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<RelativeLayout
android:id="@+id/parent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF333333"
>
<LinearLayout
android:id="@+id/myLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#FF999999"
>
<Button
android:id="@+id/alignTop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Align Parent Top"/>
<Button
android:id="@+id/alignBottom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Align Parent Bottom"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
0 Response to "Set RelativeLayout.ALIGN_PARENT_TOP/RelativeLayout.ALIGN_PARENT_BOTTOM using Java code"
Posting Komentar