在 android 中实现两个线性布局之间的关系
Implement a relationship between two linearlayouts in android
我的应用程序中有两个并排的 LinearLayouts。在第一个 LinearLayout(左侧)中,我有一些名字 lists.I 想要实现它们之间的连接或关系,当我单击其中一个名称列表项时,将显示一条文本消息或一些设计进入第二个线性布局(右侧)。
我已经用 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>
对于要在 Java 代码中使用的每个视图,您都应该有一个 ID。然后您可以在 activity 中使用 findViewById 方法找到该视图。
所以对于您的视图,您应该考虑为您的第二个 textView 使用一个 id,例如 tvDisplay,然后在您的 onClickListener 中,您应该通过这样的代码将文本设置到您的 textView 中:
((TextView)findViewById(R.id.tvDisplay)).setText("hello")
假设您已经实现了检查列表项是否被点击的代码,您只需要为目标文本视图分配一个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");
注意:该代码并非用于复制粘贴。这是您可以采取的建议或方法。
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
textView.setText(listview.getItemAtPosition(position).toString());
}
使用此代码并实现列表视图点击侦听器
你可以用这种方式来实现。
在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();
}
});
我的应用程序中有两个并排的 LinearLayouts。在第一个 LinearLayout(左侧)中,我有一些名字 lists.I 想要实现它们之间的连接或关系,当我单击其中一个名称列表项时,将显示一条文本消息或一些设计进入第二个线性布局(右侧)。 我已经用 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>
对于要在 Java 代码中使用的每个视图,您都应该有一个 ID。然后您可以在 activity 中使用 findViewById 方法找到该视图。 所以对于您的视图,您应该考虑为您的第二个 textView 使用一个 id,例如 tvDisplay,然后在您的 onClickListener 中,您应该通过这样的代码将文本设置到您的 textView 中:
((TextView)findViewById(R.id.tvDisplay)).setText("hello")
假设您已经实现了检查列表项是否被点击的代码,您只需要为目标文本视图分配一个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");
注意:该代码并非用于复制粘贴。这是您可以采取的建议或方法。
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
textView.setText(listview.getItemAtPosition(position).toString());
}
使用此代码并实现列表视图点击侦听器
你可以用这种方式来实现。
在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();
}
});