【问题标题】:Implement a relationship between two linearlayouts in android在android中实现两个线性布局之间的关系
【发布时间】:2018-09-14 06:07:46
【问题描述】:

我的应用程序中并排有两个 LinearLayout。在第一个线性布局(左侧)中,我有一些名称列表。我想以这样的方式实现它们之间的连接或关系,当我单击其中一个名称列表项时,一条文本消息或一些设计将显示到第二线性布局(右侧)。 我已经用 Toast 实现了 OnClickListener,但我不知道在第二个线性布局中显示文本消息或一些设计。 提前感谢您的帮助!

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:id="@+id/relLayoutMiddle"  android:orientation="horizontal"
            android:layout_below="@+id/relLayoutTopBar"
            android:layout_above="@+id/relLayoutBottomBar">

            <!-- Items List-->
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="80dp"
                android:layout_height="fill_parent"
                android:layout_marginRight="5dp"
                android:background="@drawable/border_design">    
                <ListView
                    android:id="@+id/item_List" android:scrollbars="none"
                    android:layout_width="match_parent"
                    android:focusable="true"
                    android:layout_height="fill_parent">
                </ListView>
            </LinearLayout>

            <!-- Display Items-->
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1" android:layout_marginLeft="5dp"
                android:background="@drawable/border_design">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="Items  will be Display here"/>
            </LinearLayout>
        </LinearLayout>

【问题讨论】:

    标签: android android-layout android-linearlayout


    【解决方案1】:

    对于要在 Java 代码中使用的每个视图,您都应该有一个 id。然后您可以在活动中使用 findViewById 方法找到该视图。 因此,对于您的视图,您应该为第二个 textView 考虑一个 id,例如 tvDisplay,然后在您的 onClickListener 中,您应该通过如下代码将文本设置到您的 textView 中:

    ((TextView)findViewById(R.id.tvDisplay)).setText("hello")
    

    【讨论】:

    • 它适用于显示短信。非常感谢@Reza.Abedini。但是如果我想实现 RecyclerView 以网格形式显示图像该怎么办?
    • 欢迎您,如果它解决了您的问题,请将此帖子作为答案。实施网格 recyclerView 请查看这篇文章:stackoverflow.com/questions/40587168/…
    【解决方案2】:

    假设你已经实现了检查列表项是否被点击的代码,你只需要为你的目标文本视图分配一个id。例如:

    <TextView
     android:id="@+id/targetID"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:text="Items  will be Display here"/>
    

    然后你需要在代码中使用它的 id 找到 TextView。例如:

    TextView tv = (TextView)View.findViewById(R.id.targetID);
    

    并使用以下方法更改电视内容:

     tv.setText("What you want to write here");
    

    注意:代码不是用来复制粘贴的。这是您可以采取的建议或方法。

    【讨论】:

      【解决方案3】:
       listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                  textView.setText(listview.getItemAtPosition(position).toString());
              }
      

      使用此代码并实现 listview 点击监听器

      【讨论】:

        【解决方案4】:

        您可以使用这种方式来实现它。

        在 XML 布局中:

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:id="@+id/relLayoutMiddle"
            android:orientation="horizontal">
        
            <!-- Items List-->
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="80dp"
                android:layout_height="fill_parent"
                android:layout_marginRight="5dp">
                <ListView
                    android:id="@+id/item_List"
                    android:scrollbars="none"
                    android:layout_width="match_parent"
                    android:focusable="true"
                    android:layout_height="fill_parent">
                </ListView>
            </LinearLayout>
        
            <!-- Display Items-->
            <LinearLayout
                android:id="@+id/ll_second"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:layout_marginLeft="5dp">
                <TextView
                    android:id="@+id/tv_message"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="Items  will be Display here"/>
            </LinearLayout>
        </LinearLayout>
        

        在代码中:

            final String[] names = {"one", "two", "three"};
            llSecond = findViewById(R.id.ll_second);
            tvMessage = findViewById(R.id.tv_message);
            ListView lvItem = findViewById(R.id.item_List);
            lvItem.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1, android.R.id.text1, names));
            lvItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    // display a text message that selected in ListView into TextView
                    tvMessage.setText(names[position]);
        
                    // Or call user defined function that make to second LinearLayout dynamically with programmatical.
                    // makeLayout();
                }
            });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-09-02
          相关资源
          最近更新 更多