简体   繁体   中英

Aligning the view to center

I want to place the two text horizontally at the center. It is very easy to put it directly in a layout. But what i am trying to achieve is :

Testapplayout.xml: This is set as content view of the activity.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<com.example.testapp.Customlayout
    android:id="@+id/custom"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginLeft="2dp"
    android:layout_marginTop="2dp">
 </com.example.testapp.Customlayout>
</LinearLayout>

class Customlayout

public class Customlayout extends LinearLayout {

public Customlayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public Customlayout(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public Customlayout(Context context) {
    this(context, null, 0);
}

}

Now on the Testapplayout, customlayout, I am trying to inflate the layout to show the texts at the center:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:gravity="center|bottom"
  android:orientation="horizontal" >

<TextView
    android:id="@+id/status_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="text1"
    android:layout_gravity="center"
    />

<TextView
    android:id="@+id/text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/status_text"
    />
   </LinearLayout>

This still comes at the

try

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/relative_layout"
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent">

 <com.example.testapp.Customlayout
   android:id="@+id/custom"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_centerInParent="true">

 </com.example.testapp.Customlayout>

</RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM