0

我有带有两个按钮和单个文本视图的命令标题。textview 位于屏幕中央。按钮 1 位于父项的右侧,按钮 2 位于父项的左侧。

现在,我想在父级的中心显示文本视图,而不是在两个按钮之间的空间的中心。

textview 中的文本长度可以是 3 个单词或超过 10 个单词。

当长度超过 10 个字时,我不想在两个按钮上方重叠 textview。而且我想要在屏幕中央的文本视图,而只有 3 个单词。

当我使用下面的代码时,当只有 3 到 4 个单词时,它不会在屏幕中央水平显示 textview,但下面的代码在超过 10 个单词时也不会重叠。

<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=".Webviewdemo" >

    <ImageView
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:background="@drawable/backbtn" />

    <ImageView
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="@drawable/infobtn" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_toLeftOf="@+id/button2"
        android:layout_toRightOf="@+id/button1"
        android:gravity="center"
        android:singleLine="true"
        android:text="123"
        android:textSize="22sp"/>

</RelativeLayout>

以同样的方式,当我使用下面的代码时,当只有 3 到 4 个单词但下面的代码重叠而有 10 个以上的单词时,它会在屏幕中央水平显示 textview。

<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=".Webviewdemo" >

    <ImageView
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:background="@drawable/backbtn" />

    <ImageView
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="@drawable/infobtn" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:singleLine="true"
        android:text="123"
        android:textSize="22sp" />

</RelativeLayout>

所以,我的问题是我怎样才能用单个代码实现以上两个目标。

  1. 当有更多单词时,Textview 不应重叠。
  2. Textview 应该在中心,而没有单词。

我希望你们都明白我的问题。如果有任何疑问请询问。

4

4 回答 4

0

试试这个(没有centerInParent

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/button2"
    android:layout_toRightOf="@+id/button1"
    android:gravity="center"
    android:singleLine="true"
    android:text="123"
    android:textSize="22sp"/>
于 2013-09-11T11:47:26.717 回答
0

试试这个代码

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:layout_toLeftOf="@+id/button2"
    android:layout_toRightOf="@+id/button1"
    android:gravity="center"
    android:singleLine="true"
    android:text="123"
    android:textSize="22sp"/>

编辑 删除此行

  android:layout_centerInParent="true"
于 2013-09-11T11:25:25.033 回答
0
   Use This Code 

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

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1" 
        android:gravity="left">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" 
        android:gravity="center">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="right" >

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

    </LinearLayout>

</LinearLayout>
于 2013-09-11T11:37:20.703 回答
0
 <LinearLayout
    android:id="@+id/panelIconLeft1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_margin="5dp" >

    <Button
        android:id="@+id/btnHome1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="LOCATION"
        android:onClick="btnHomeClick" />
</LinearLayout>

<TextView
    android:id="@+id/txtHeading1"
  style="@style/heading_text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_toLeftOf="@+id/panelIconRight1"
    android:layout_toRightOf="@id/panelIconLeft1"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:gravity="center"

    android:singleLine="true"
    android:text=""
    android:textColor="@android:color/white" />

<LinearLayout
    android:id="@+id/panelIconRight1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_margin="5dp" >

    <Button
        android:id="@+id/btnFeedback1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Post Ad"
        android:onClick="btnFeedbackClick" />
</LinearLayout

上面的代码会帮助你

于 2013-09-11T12:31:18.163 回答