【问题标题】:Textview center-aligned based on percentage of screenwidth基于屏幕宽度百分比的Textview居中对齐
【发布时间】:2014-05-31 23:00:39
【问题描述】:

我以为这将是一个常见问题,但我找不到任何答案......

是否可以根据屏幕宽度的百分比将(可变长度)文本视图居中对齐?

就我而言,我有 2 个文本视图,我想实现这样的目标:

另一个例子:

第一个文本视图居中对齐屏幕宽度的 30%,width=wrap_content,而第二个文本视图占据了其余空间。第一个 textview 的大小不应该是固定的(见上图)

我不想以编程方式执行此操作,因为这对我的程序来说会很昂贵。如果可能,最好放在 xml 布局文件中:)

现在我只能使用 LinearLayout 的 layout_weight 实现“左对齐”文本视图(通过在左侧放置 30% 的虚拟对象),但我想要居中对齐。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="3" >
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="7"
        android:orientation="horizontal" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="textview" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:singleLine="true"
            android:text="another textview"
            android:textColor="#FFFF0000"/>
    </LinearLayout>
</LinearLayout>

【问题讨论】:

  • 发布您的布局文件内容!
  • @mansoulx 我没有布局,因为我想不出答案...但是让我发布我对左对齐案例的尝试。

标签: android android-layout textview percentage


【解决方案1】:

经过将近一个小时的测试,这个“...”布局,我成功了:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="100" >

    <RelativeLayout
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="40"
        android:background="@android:color/transparent">

        <RelativeLayout
            android:id="@+id/left_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_margin="0dp"
            android:gravity="center"
            android:padding="0dp"
            android:singleLine="true"
            android:text="first textview"
            android:textColor="#FFFF0000" >

            <View
                android:layout_width="1dp"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:background="#0000FF" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:background="#00FF00"
                android:gravity="center"
                android:text="textview"
                android:textColor="#FFF" />
        </RelativeLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@id/left_layout"
            android:background="#0000FF"
            android:gravity="center"
            android:text="textview"
            android:textColor="#FFF" />
    </RelativeLayout>

    <TextView
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="60"
        android:background="#FFFF0000"
        android:gravity="right"
        android:singleLine="true"
        android:text="another textview"
        android:textColor="#FFF" />

</LinearLayout>

输出

最后在你的 Java 中,根据绿色的 textview 宽度或内容长度,你可以通过编程改变其他 textview 的 layout_weight。

This should help you

祝你有美好的一天。

【讨论】:

  • 对不起,但这完全不是我想要的(只是在 Eclipse 上测试过)。 Textview1 占据了屏幕宽度的 70%,这不是我想要的(参考我的图片)。可以发个截图吗?
  • 对不起,伙计,我忘记了您问题的主要问题。让我编辑答案
  • 答案已编辑。让我知道它有效。我还没有测试过。
  • 虽然这是一个不错的方法(我认为你需要 60:40)但我不能在 textview1 和 textview2 之间留出空间,这是困难的部分:(
  • 您是否尝试过使用margin left/rightpadding left/right,或者在两个TextView 之间添加View
【解决方案2】:
// try this way hope this will help you...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <View
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_weight="0.15"/>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.85">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="left"
                android:text="first Textview"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="second textview demo text "/>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>    

【讨论】:

  • 这不起作用,因为当第一个 textview 的内容小于最大宽度时,文本会右对齐,而不是居中对齐...我的第一个 textview 通常只包含长度为 1 - 5 的单词
  • 现在你检查我已经编辑了我的ans并告诉我仍然没有达到你的要求......
  • @ Haresh 不,现在 textview1 不是右对齐,而是左对齐,仍然不是居中! (我的文本视图会改变,假设当你输入“1、2、一、四、三”时它应该是中心
【解决方案3】:

试试这个

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="3"
    android:gravity="center_horizontal"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
</LinearLayout>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="7"
    android:gravity="center_horizontal"
    android:orientation="vertical" >

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

工作完美由我检查。

【讨论】:

  • 请仔细检查我的问题和我的图片。我不想要文本视图之间的空间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-09
  • 2013-11-29
  • 2011-12-21
  • 2012-08-05
  • 1970-01-01
  • 2017-05-07
  • 1970-01-01
相关资源
最近更新 更多