Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.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中在同一行中显示两个文本视图_Android_Android Layout_Android Intent_Android Widget - Fatal编程技术网

如何在Android中在同一行中显示两个文本视图

如何在Android中在同一行中显示两个文本视图,android,android-layout,android-intent,android-widget,Android,Android Layout,Android Intent,Android Widget,我有我的布局xml文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Tex

我有我的布局xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
     android:id="@+id/titlename"  
android:layout_width="wrap_content"
android:layout_height="wrap_content" 
android:text="@string/HostName"
 android:layout_weight="0"

/>
<TextView
     android:id="@+id/name"  
android:layout_width="wrap_content"
android:layout_height="wrap_content" 
 android:layout_weight="0"
/>
</LinearLayout>
有人能帮忙吗?

使用以下代码

已更新

 <?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:orientation="horizontal"
   android:weightSum="1" >
<TextView
    android:id="@+id/titlename"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:gravity="center"
    android:text="hi" />
<TextView
    android:id="@+id/name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:gravity="center"
    android:text="2hi2" />
 </LinearLayout>

我会去掉
权重属性。我唯一能看到的就是把事情搞砸了。还要注意的是,不推荐使用
fill\u parent
,您应该使用
match\u parent

或者,如果出于某种原因需要
重量
,则如果希望每个重量占据屏幕的一半,请将其更改为1


另外,如果使用
weight
,则应设置
android:layout\u width=“0dp”

为线性布局设置
android:orientation=“horizontal”

如下更新布局,除非在父线性布局上设置了权重和,否则不希望包含weight=0属性。加上权重和权重总和并不真正符合你的要求

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView
         android:id="@+id/titlename"  
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" 
         android:text="@string/HostName"

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

您可以尝试使用RelativeLayout


这样,您就可以在titlename元素上设置
alignParentTop=“true”
,并将name
alignTo=“@id/titlename”
toRightOf=“@id/titlename”
在name元素上设置为android:layout\u宽度为0dp。然后为每个文本视图设置0.5个权重。每个文本视图将填充父布局的一半宽度。比如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
     android:id="@+id/titlename"  
android:layout_width="0dp"
android:layout_height="wrap_content" 
android:text="@string/HostName"
 android:layout_weight="0.5"

/>
<TextView
     android:id="@+id/name"  
android:layout_width="0dp"
android:layout_height="wrap_content" 
 android:layout_weight="0.5"
/>
</LinearLayout>

实际上,您的原始代码在我的ADT中工作得非常完美。您是否在Java代码中更改了文本大小或行填充,从而更新了第二个文本视图?


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

<TextView
     android:id="@+id/titlename"  
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" 
     android:text="@string/HostName"
/>
<TextView
     android:id="@+id/name"  
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" 
/>
</LinearLayout>

创建一个
线性布局
将2个
文本视图
包围起来

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnLogin"
        android:layout_below="@id/etPassword"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:text="Login"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/btnLogin"
        android:layout_marginTop="10dp"
        android:gravity="center">

        <TextView
            android:id="@+id/tvIAlwaysHaveAccount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/btnLogin"
            android:text="I always have an account?" />

        <TextView
            android:id="@+id/tvForgotPassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/tvIAlwaysHaveAccount"
            android:layout_marginLeft="15dp"
            android:text="Forgot password?" />

    </LinearLayout>


</RelativeLayout>

你的节目也一样:“


您是否尝试过android:layout\u weight=“0.5”同时显示两个文本视图?这将使两个视图的宽度都减半?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnLogin"
        android:layout_below="@id/etPassword"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:text="Login"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/btnLogin"
        android:layout_marginTop="10dp"
        android:gravity="center">

        <TextView
            android:id="@+id/tvIAlwaysHaveAccount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/btnLogin"
            android:text="I always have an account?" />

        <TextView
            android:id="@+id/tvForgotPassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/tvIAlwaysHaveAccount"
            android:layout_marginLeft="15dp"
            android:text="Forgot password?" />

    </LinearLayout>


</RelativeLayout>