gpt4 book ai didi

android - 当我在 android 中点击它时,如何在 Button 下方显示 SpinnerList

转载 作者:行者123 更新时间:2023-11-30 01:28:45 26 4
gpt4 key购买 nike

嗨,我是 Android 的新手,我在我的应用程序中创建了一个按钮和一个 TextView

当我们点击我的按钮时,我想显示微调列表,当我们选择任何一行时,我想在我的 TextView 中显示该数据

为此,我写了下面的代码,但是当我们点击按钮时,微调器不会显示在按钮下方

它显示如下图,我想显示按钮下方的列表,请帮助我

xml:-

<?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:id="@+id/mainlayout"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal">

<TextView
android:textSize="20dp"
android:id="@+id/title"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:layout_gravity="center_vertical"
android:gravity="center"
android:text="title" />

<Button
android:id="@+id/imageViewplaces"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="20dp"
android:text="OK"
android:onClick="ClickButton" />

</LinearLayout>

<Spinner
android:layout_below ="@+id/mainlayout"
android:layout_marginRight="10dp"
android:id="@+id/spinner"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:prompt="@string/spinner_title" />

</LinearLayout>

Activity 类:-

public class SpinnerList extends AppCompatActivity {

String [] spinnerList = {"first","second","third"
};
Spinner spinner;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner_layout);

spinner = (Spinner)findViewById(R.id.spinner);
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerList);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
spinner.setVisibility(spinner.GONE);
}

public void ClickButton(View view){

spinner.performClick();
}

enter image description here

最佳答案

it looks like this给你:

<?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="match_parent">

<LinearLayout
android:id="@+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@id/imageViewplaces"
android:orientation="horizontal"
android:paddingBottom="15dp"
android:paddingTop="15dp">

<TextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="center"
android:text="title"
android:textSize="20dp" />

</LinearLayout>


<Button
android:id="@+id/imageViewplaces"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_marginTop="15dp"
android:layout_marginRight="15dp"
android:onClick="ClickButton"
android:text="OK" />

<Spinner
android:id="@+id/spinner"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_below="@+id/imageViewplaces"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
/>
</RelativeLayout>

关于android - 当我在 android 中点击它时,如何在 Button 下方显示 SpinnerList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36058628/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com