Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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/6/asp.net-mvc-3/4.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
Android 在垂直和水平中心设置TextView_Android - Fatal编程技术网

Android 在垂直和水平中心设置TextView

Android 在垂直和水平中心设置TextView,android,Android,我需要在垂直和水平的中心设置textView10。我试了很多,但都没有结果。。。我尝试过重力选项,但没有。。有什么想法吗?textView10位于scrollview中。谢谢你的帮助 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pare

我需要在垂直和水平的中心设置textView10。我试了很多,但都没有结果。。。我尝试过重力选项,但没有。。有什么想法吗?textView10位于scrollview中。谢谢你的帮助

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:background="#000000"
     android:orientation="vertical" >

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

  <TextView
      android:layout_width="151dp"
      android:layout_height="fill_parent"
      android:textSize="15pt" />

  <ScrollView
      android:id="@+id/scrollView10"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:background="#000000" >

  <TextView
      android:id="@+id/textView10"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#000000"
      android:gravity="center_vertical"
      android:textAlignment="center"
      android:textColor="#ffffff"
      android:textSize="13sp"
      android:textStyle="italic" />

  </ScrollView>      

 </LinearLayout>

     <Button
         android:id="@+id/button10"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="Chiudi" />

</LinearLayout>

将外部布局更改为RelativeLayout,并将yout ScrollView定位在父中心。将其他元素相对于一个元素放置在中心

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="horizontal" >
    <TextView
        android:id="@+id/TextView1"
        android:layout_width="151dp"
        android:layout_height="fill_parent"
        android:textSize="15pt" />
    <ScrollView
        android:id="@+id/scrollView10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="#000000" >
        <TextView
            android:id="@+id/textView10"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#000000"
            android:gravity="center_vertical"
            android:textAlignment="center"
            android:textColor="#ffffff"
            android:textSize="13sp"
            android:textStyle="italic" />
    </ScrollView>
</RelativeLayout>

将TextView放在RelativeLayout内,并将RelativeLayout放在ScrollView内

然后,在文本视图中设置android:layout\u centerInParent=“true”
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:orientation="vertical" >

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

    <TextView
        android:layout_width="151dp"
        android:layout_height="fill_parent"
        android:textSize="15pt" />

    <ScrollView
        android:id="@+id/scrollView10"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#000000" >

        <TextView
            android:id="@+id/textView10"
            android:layout_width="match_parent"
            android:layout_height="320dp"
            android:background="#000000"
            android:gravity="center"
            android:textColor="#ffffff"
            android:textSize="13sp"
            android:text="Hi What is your name"
            android:textStyle="italic" />
    </ScrollView>
</LinearLayout>

<Button
    android:id="@+id/button10"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Chiudi" />

+1因为此解决方案不涉及文本视图的固定布局高度。外部RelativeLayout可以设置为任何大小/位置,并且仍然有效。