使用相对布局

时间:2018-02-01 09:32:05

标签: android android-layout textview android-relativelayout

我在Android Studio上的应用程序中遇到了xml布局的问题。 在Android Studio上它向我展示了我想要的东西,但是当我运行应用程序时,布局绝对不是我想要的。一切都搞砸了,我不明白为什么。

我一直在Google上搜索并查看论坛一段时间,但到目前为止我一无所获。我花了很多时间在Android开发人员的教程和文档上,但仍然坚持。

所以这是我的xml文件,Android Studio在设计视图中向我展示了我想要的布局:

    <?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="80dp"
    android:background="@color/white"
    android:paddingLeft="@dimen/_10sdp"
    android:paddingRight="@dimen/_10sdp"
    android:id="@+id/ll_printer_name">
   <TextView
        android:id="@+id/tv_name_printer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Printer 1"
        android:textSize="10sp"
        android:textAllCaps="true"
        android:textAlignment="center"
        android:textAppearance="@style/TextAppearance.Bold"
        android:textColor="@color/text_grey"
        android:gravity="center"
        android:layout_centerInParent="true"
        android:layout_centerVertical="true"/>

    <TextView
        android:id="@+id/tv_press_to_test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_name_printer"
        android:text="press to test"
        android:textAlignment="center"
        android:textAllCaps="true"
        android:textAppearance="@style/TextAppearance.Regular"
        android:textColor="@color/text_grey"
        android:textSize="6sp" />

    <Space
        android:layout_below="@id/tv_press_to_test"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

Here is the picture of the design view on Studio Android. It is as I want to be.

当我运行应用程序时,就像这样:

What I have after running the app

textView“按测试”不是我想要的地方,即使我在XML文件上写了这个,也取决于“打印机1”:

android:layout_below="@id/tv_name_printer"

如果有人可以提供帮助,我会被困一段时间。 在此先感谢!!

编辑(特别是对于tanni Tanna):这里的代码的一部分(代码非常长)使用此布局来创建显示不符合我想要的视图的片段: / p>

因此,当我们点击按钮时,会调用此方法。此方法使用上面显示的布局(布局的id:item_printer_name)

 private ViewGroup llPrinterList; 


 private void searchPrinters(){
    PrinterSearcher.searchPrintersListener listener = new PrinterSearcher.searchPrintersListener() {
        @Override
        public void printersFound(List<Printer> printers) {
            for(Printer p : printers){
                View llPrinterName = LayoutInflater.from(getContext()).inflate(R.layout.item_printer_name, null);
                llPrinterList.addView(llPrinterName);
                TextView t = (TextView) findViewById(R.id.tv_name_printer);
                t.setText(p.getName());
            }
        }
    };
 PrinterSearcher.searchPrinters(listener, this, getActivity());
}

PrinterSearcher是一个用于在网络中查找打印机的类。我们真的不需要了解太多。 searchPrintersListener是此类中的一个接口。这个界面只有一个方法:printersFound

但我也发现了一些奇怪的事情:当我这样做(我已经删除了空间,但实际上我需要这个空间)在我的布局中它或多或少地做了我想要的东西:

   <?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="80dp"
    android:background="@color/white"
    android:paddingLeft="@dimen/_10sdp"
    android:paddingRight="@dimen/_10sdp"
    android:id="@+id/ll_printer_name">
    <TextView
       android:id="@+id/tv_name_printer"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="Printer 1"
       android:textSize="10sp"
       android:textAllCaps="true"
       android:textAlignment="center"
       android:textAppearance="@style/TextAppearance.Bold"
       android:textColor="@color/text_grey"
       android:gravity="center"
       android:layout_centerInParent="true"
       android:layout_centerVertical="true"/>

    <TextView
      android:id="@+id/tv_press_to_test"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_below="@+id/tv_name_printer"
      android:text="press to test"
      android:textAlignment="center"
      android:textAllCaps="true"
      android:textAppearance="@style/TextAppearance.Regular"
      android:textColor="@color/text_grey"
      android:textSize="6sp" />
 </RelativeLayout>

但是当我改变textView“按测试”时:

android:layout_height="wrap_content"

到:

android:layout_height="match_parent"

它与之前的事情相同

3 个答案:

答案 0 :(得分:0)

问题android:layout_below =“@ id / tv_name_printer” 在第二篇TextVIew中使用以下

android:layout_below="@+id/tv_name_printer" 

答案 1 :(得分:0)

使用此

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_printer_name"
android:layout_width="match_parent"
android:layout_height="80dp"
>

<TextView
    android:id="@+id/tv_name_printer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_centerVertical="true"
    android:gravity="center"
    android:text="Printer 1"
    android:textAlignment="center"
    android:textAllCaps="true"
    android:textSize="10sp" />

<TextView
    android:id="@+id/tv_press_to_test"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tv_name_printer"
    android:text="press to test"
    android:textAlignment="center"
    android:textAllCaps="true"
    android:textSize="6sp" />

<Space
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/tv_press_to_test" />

编辑:这是结果 enter image description here

答案 2 :(得分:0)

<?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="80dp"
android:background="@color/white"
android:paddingLeft="10sp"
android:paddingRight="10sp"
android:id="@+id/ll_printer_name">
<TextView
    android:id="@+id/tv_name_printer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Printer 1"
    android:textSize="10sp"
    android:textAllCaps="true"
    android:textAlignment="center"
    android:gravity="center"
    android:layout_centerInParent="true"
    android:layout_centerVertical="true"/>

<TextView
    android:id="@+id/tv_press_to_test"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/tv_name_printer"
    android:text="press to test"
    android:textAlignment="center"
    android:gravity="center"
    android:textAllCaps="true"
    android:textSize="6sp" />

<Space
    android:layout_below="@id/tv_press_to_test"
    android:layout_width="match_parent"
    android:layout_height="match_parent" /></RelativeLayout>