Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.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/206.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 展开文本以匹配父宽度_Java_Android_User Interface_Textview - Fatal编程技术网

Java 展开文本以匹配父宽度

Java 展开文本以匹配父宽度,java,android,user-interface,textview,Java,Android,User Interface,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" android:paddingBottom="@di

请看一下下面的代码

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".SalesInqury" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="@string/sales_inqury"
        android:textSize="40sp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView1"
        android:layout_marginLeft="21dp"
        android:layout_toRightOf="@+id/textView1"
        android:src="@drawable/add_2" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_alignParentRight="true"
        android:layout_marginTop="16dp"
        android:hint="Search"
        android:ems="8" >

        <requestFocus />
    </EditText>

</RelativeLayout>

这将生成以下输出

老实说,这不是我想要的。文本应“展开”以匹配屏幕宽度,直到找到图像视图。我认为改善字母间距可能是解决办法


但是我想知道是否有更好的解决方案,我们可以用代码或XML来实现。

如果我没弄错,您需要“销售查询”下面的编辑文本

指示视图应分布在布局的整个宽度上

但是如果你想让EditText和“销售查询”在同一行,那么这行就完成了

<LinearLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="horizontal"
    tools:context=".SalesInqury" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:drawableRight="@drawable/add_2"
        android:text="@string/sales_inqury"
        android:textSize="40sp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="center_vertical"
        android:layout_marginTop="16dp"
        android:hint="Search"
        android:ems="8" >

        <requestFocus />
    </EditText>

</LinearLayout>

使用水平线性布局,设置第一个文本视图的布局宽度以包装内容。
EditText应使用layout_weight=“1”(仅适用于LinearLayout),这意味着它将占用尽可能多的可用空间。在水平线性布局中使用布局权重时,将布局宽度设置为0dp。如果是垂直线性布局,布局高度应为0dp。

嘿,我在代码中做了一些更改。我使用LinerLayout而不是相对布局

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

    android:orientation="horizontal"
    android:weightSum="3.0f"
     >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1.0f"
        android:textSize="40sp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1.0f"
        android:layout_marginLeft="21dp"

        />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1.0f"
        android:hint="Search"
        android:ems="8" >

        <requestFocus />
    </EditText>

</LinearLayout>


您使用的包裹内容将根据其大小获取区域,而不是此匹配父对象/填充父对象可以更友好地帮助您您应该将ImageView替换为复合drawable。也。我不知道这是否是他想要的。我以为他想要下面的编辑文本“销售查询”。此外,如果在水平线性布局中使用布局权重,则应设置布局宽度=“0dp”
<LinearLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="horizontal"
    tools:context=".SalesInqury" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:drawableRight="@drawable/add_2"
        android:text="@string/sales_inqury"
        android:textSize="40sp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="center_vertical"
        android:layout_marginTop="16dp"
        android:hint="Search"
        android:ems="8" >

        <requestFocus />
    </EditText>

</LinearLayout>
<LinearLayout 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"

    android:orientation="horizontal"
    android:weightSum="3.0f"
     >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1.0f"
        android:textSize="40sp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1.0f"
        android:layout_marginLeft="21dp"

        />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1.0f"
        android:hint="Search"
        android:ems="8" >

        <requestFocus />
    </EditText>

</LinearLayout>