Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.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/0/xml/13.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_Xml_Android Layout_Android Linearlayout - Fatal编程技术网

Android 安卓在线性布局内对齐视图,无需推离屏幕。

Android 安卓在线性布局内对齐视图,无需推离屏幕。,android,xml,android-layout,android-linearlayout,Android,Xml,Android Layout,Android Linearlayout,我正在创建一个包含如下项目列表的应用程序: |-----------------------------------------------------| |TextView--View--View--View---------------------------| |-----------------------------------------------------| |-----------------------------------------------------| |Te

我正在创建一个包含如下项目列表的应用程序:

|-----------------------------------------------------|
|TextView--View--View--View---------------------------|
|-----------------------------------------------------|

|-----------------------------------------------------|
|TextViewTextViewTextView--View--View--View-----------|
|-----------------------------------------------------|

|-----------------------------------------------------|
|TextViewTextViewTextViewTextView..--View--View--View-|
|-----------------------------------------------------|
但在我的第三排文本视图中,我将三个视图推远:

|--------------------------------------------------|
|TextViewTextViewTextViewTextViewTextView--View--Vi|
|--------------------------------------------------|
我的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

      <TextView
        android:id="@+id/TextViewSubject"
        android:text="SUBJECT"
        android:layout_weight="1"
        android:singleLine="true"
        android:ellipsize="end"
        android:layout_alignParentLeft="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>


      <View
          android:background="#FF0000"
          android:layout_weight="0"
          android:layout_width="10dp"
          android:layout_height="10dp"/>
      <View
          android:layout_weight="0"
          android:background="#00FF00"
          android:layout_width="10dp"
          android:layout_height="10dp"/>
      <View
          android:layout_weight="0"
          android:background="#0000FF"
          android:layout_width="10dp"
          android:layout_height="10dp"/>

  </LinearLayout>

</LinearLayout>

此外,我还尝试使用RelativeLayout,但以这种方式,3个视图始终与右侧父视图对齐。我希望这3个视图遵循文本视图,而不是被推离屏幕


这可能吗?任何帮助都将不胜感激。

在第二个线性布局中添加
android:weightSum=4
并在每个子视图中创建
android:layout\u weight=1
,确保在子视图中将
android:layou width
设置为0dp

尝试在线性布局中使用权重

在这里,我已经修复了它,你可以改变重量来增加按钮的大小

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

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

    <TextView
        android:id="@+id/TextViewSubject"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_weight="27"
        android:ellipsize="end"
        android:singleLine="true"
        android:text="SUBJECT" />


    <View
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="10dp"
        android:background="#FF0000" />

    <View
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="10dp"
        android:background="#00FF00" />

    <View
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="10dp"
        android:background="#0000FF" />

</LinearLayout>


试试这个,希望这就是你想要的。我刚去掉了重量线。祝你好运




如果设备上的空间不可用,那么如何将它们全部安装进去。如果文本视图覆盖了75%的屏幕,那么超过25%的视图又如何适应呢。即使你设法适应这个屏幕大小,但在较小的屏幕上,它仍然可能溢出。@AbhinavPuri是的。我明白这一点。我希望这3个视图将始终显示在屏幕上,并在这3个视图的左侧留出可用空间。如果没有足够的空间,文本视图可能不可见。在这种情况下,布局中的所有视图都将具有相同的宽度。我需要3个固定宽度的视图(10dp)。例如,如果您希望文本视图比视图大5倍,则可以将权重总和设置为8,将文本视图权重设置为5,将每个视图设置为1,但如果确实希望10dp,请使用RelativeLayout,将视图以10dp固定宽度向右对齐,并将文本视图设置为填充左侧->向左对齐,然后设置
android:layout\u right=“右侧视图的id”
。希望这将有助于我使用你的代码。3个视图与右侧对齐。我需要将它们对齐到文本视图的左侧。您需要将视图对齐到文本视图的左侧。如果您要向左对齐,而不是将视图放在文本视图上方,则所有视图都将对齐到左侧我认为我们彼此不理解。我想把第一个文本视图,然后在这个文本视图的右边3个视图。即使textview很长,3个视图也应该始终显示在屏幕上。我也尝试在不增加权重的情况下执行此操作,但这不适用于长textview。我们已经使用了`android:singleLine=“true”`所以我们的textview不应该那么长。但是如果你想阻止这一点,你可以用“android:maxLength=“30”来限制你的文本长度。因此,如果您有一个长文本,它将只显示30个字符。
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/TextViewSubject"
            android:text="SUBJECT"
            android:singleLine="true"
            android:ellipsize="end"
            android:layout_alignParentLeft="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>


        <View
            android:background="#FF0000"
            android:layout_width="10dp"
            android:layout_height="10dp"/>
        <View
            android:background="#00FF00"
            android:layout_width="10dp"
            android:layout_height="10dp"/>
        <View
            android:background="#0000FF"
            android:layout_width="10dp"
            android:layout_height="10dp"/>

    </LinearLayout>

</LinearLayout>
<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView
        android:text="largeTextlargeTextlargeTextlargeTextlargeTextlargeText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:ellipsize="end"
        android:maxLines="1"
        android:paddingRight="95dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView"
        android:orientation="horizontal">
    <View
        android:background="@android:color/black"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:id="@+id/button3" />
    <View
        android:background="@android:color/black"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:id="@+id/button2" />
    <View
        android:background="@android:color/black"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:id="@+id/button" />
    </LinearLayout>
</RelativeLayout>