如何使用LinerLayout对齐ImageButton左侧和TextView中心?

时间:2016-01-29 10:13:26

标签: android layout textview imagebutton

<?xml version="1.0" encoding="utf-8"?>  

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.ravi.designdemo.Main4Activity">  

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/navigationbar"
        android:id="@id/ll1">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imb1"
            android:background="@drawable/back" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv1"
            android:text="Sign In"
            android:textColor="#ffffff"
            android:gravity="center_vertical"
            android:textSize="25dp"
            android:textStyle="bold"/>

    </LinearLayout>

</RelativeLayout>

这是我的布局。我在左侧有一个ImageView,在LinearLayout的中心有一个TextView。我正在使用LinearLayout Inside RelativeLayout。

3 个答案:

答案 0 :(得分:0)

如果您的 LinearLayout 是垂直的

STDOUT

然后你可以将 android :: gravity 参数设置为 left center

否则,如果 ImageButton TextView 处于同一级别,那么您不需要使用 LinearLayout ,尤其是如果它在< em> RelativeLayout 并使用相对布局参数将 ImageView 对齐到左边,将 TextView 对齐到它的中心。

例如,我将删除 LinearLayout 并执行类似的操作:

android:orientation="vertical"

此解决方案的线索是使用

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

    <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imb1"
            android:background="@drawable/back"/>

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#FF00"
            android:text="Sign In"
            android:textColor="#ffffff"
            android:textSize="25dp"
            android:layout_centerHorizontal="true"
            android:textStyle="bold"/>

</RelativeLayout>

因为在 RelativeLayout 这是我正确的中心视图方式。

答案 1 :(得分:0)

机器人:layout_gravity =&#34; center_vertical&#34; 将其添加到Textview。

答案 2 :(得分:0)

在“线性布局”中,子元素看起来像是一个接一个。根据您的问题,您可以使用相对布局来完成此操作,但在线性布局中则无法实现。

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    tools:context="com.example.ravi.designdemo.Main4Activity">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imb1"
            android:layout_alignParentLeft="true"
            android:background="@mipmap/ic_launcher" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv1"
            android:text="Sign In"
            android:textColor="#ffffff"
            android:layout_centerInParent="true"
            android:textSize="25dp"
            android:textStyle="bold"/>

</RelativeLayout>
相关问题