Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';t使用RelativeLayout将文本视图居中_Java_Android_Android Relativelayout - Fatal编程技术网

Java Can';t使用RelativeLayout将文本视图居中

Java Can';t使用RelativeLayout将文本视图居中,java,android,android-relativelayout,Java,Android,Android Relativelayout,我的文本视图有问题,我就是无法将其居中。下面是它的外观: 实际上它看起来是这样的 这是我的XML代码 <?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="m

我的文本视图有问题,我就是无法将其居中。下面是它的外观:

实际上它看起来是这样的

这是我的XML代码

   <?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">

    <TextView
        android:id="@+id/magnitudeTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_margin="10dp"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/cityNameTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_margin="10dp"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/dateTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_margin="10dp"
        android:textSize="15sp" />

</RelativeLayout>

水平线性布局将为您的问题提供更好、更轻的解决方案。您要做的是将三个文本视图包装在其中,然后对左侧和右侧的文本视图使用
layout\u width=wrap\u content
layout\u width=0dp
布局重量=1


对于中间的一个

通常,带有权重的线性布局会满足这些类型的需求,系统会根据您提到的权重和自动将屏幕划分为多个部分

  <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:weightSum = "5"
     android:orientation = "horizontal"
     />

    <TextView
      android:id="@+id/magnitudeTV"
      android:layout_width="0dp"
      android:layout_weight = "1"
      android:layout_height="wrap_content"
      android:layout_alignParentStart="true"
      android:layout_alignParentLeft="true"
      android:layout_margin="10dp"
      android:textSize="15sp" />

    <TextView
      android:id="@+id/cityNameTV"
      android:layout_width="0dp"
      android:layout_weight = "3"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_margin="10dp"
      android:textSize="15sp" />

    <TextView
      android:id="@+id/dateTV"
      android:layout_width="0dp"
      android:layout_weight = "1"
      android:layout_height="wrap_content"
      android:layout_alignParentEnd="true"
      android:layout_alignParentRight="true"
      android:layout_margin="10dp"
      android:textSize="15sp" />

</LinearLayout>


下面的技术对我很有用。那就是:
使用第一个
TextView
作为
left
方向和中间
TextView
作为
center
方向和最后一个
TextView
作为
right
方向,并为每个
TextView
应用
权重

您的代码是这样的:

      <?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"
        orientation=vertical
        weightsum=3
>

        <TextView
            android:id="@+id/magnitudeTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_margin="10dp"
            android:textSize="15sp"
            android:layout_weight=1 />

        <TextView
            android:id="@+id/cityNameTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:textSize="15sp" 
            android:layout_weight=1/>

        <TextView
            android:id="@+id/dateTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_margin="10dp"
            android:textSize="15sp" 
            android:layout_weight=1/>
    </LinearLayout>


`

使用水平方向的
线性布局
以及儿童文本视图的
权重总和
为3,将为您提供一个轻巧、更可靠的解决方案,如下所示:

               <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:weightSum="3"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/magnitudeTV"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="10dp"
                        android:layout_weight="1"
                        android:text="test"
                        android:textSize="15sp" />

                    <TextView
                        android:id="@+id/cityNameTV"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="test"
                        android:gravity="center"
                        android:layout_weight="1"
                        android:layout_margin="10dp"
                        android:textSize="15sp" />

                    <TextView
                        android:id="@+id/dateTV"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:layout_margin="10dp"
                        android:textSize="15sp" />

                </LinearLayout>


现在,您的每个文本视图将占据整个屏幕宽度的1/3,如果您希望中间的视图更大,您可以在每个文本视图中使用
布局权重
值来匹配您所需的设计。

您可以通过以下操作来实现:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true">

    <TextView
        android:id="@+id/magnitudeTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="sdf34 "
        android:textColor="#000000"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/cityNameTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/magnitudeTV"
        android:layout_margin="10dp"
        android:text="dasdasdsadasdds"
        android:textColor="#000000"
        android:textSize="15sp" />

    <TextView
        android:layout_toRightOf="@id/cityNameTV"
        android:id="@+id/dateTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="fg25111dfg"
        android:textColor="#000000"
        android:textSize="15sp" />

</RelativeLayout>




but according to me it is not a good solution for this you can con-catenate all the three strings and than set the final string to one single text view as mentioned below:


  String final_string = distance +" "+string_2  +" "+string_3;
    textView.setText(final_string);

但据我所知,这不是一个很好的解决方案,您可以连接所有三个字符串,然后将最终字符串设置为一个文本视图,如下所述:
字符串最终\u字符串=距离+“”+字符串\u 2+“”+字符串\u 3;
textView.setText(最终_字符串);

您可以使用
LinearLayout
而不是
RelativeLayout
,您可以在其中将
weightsum
设置为3,这将为您提供更可靠的解决方案。每个
TextView
都将获得相等的行空间。

android:layout\u centerInParent=“true”和android:gravity=“center”缺失。如何膨胀列表项?请为每个TextView添加layout\u weight=1,它将根据其权重自动占用空间。。我相信这会解决你的问题。根据你的喜好修补边距和重量,这是一般的想法