#android
Вопрос:
У меня есть 2 сомнения —
1>как разместить Imageview и textview по горизонтали, я использовал android:ориентация=»горизонтальная»>> но выравнивание по вертикали>>
2> управление вращателем показывает только 1 значение, хотя я ввел 5 значений; другие не выбраны
В основной деятельности я встраиваю прядильщик и редактирую текст внутри представления карты. В представлении карты я встраиваю изображение флага в представление изображения и текстовое представление, в котором отображается код страны ISD.
Я хочу этого->>
но получить это->
код:
MainActivity:
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
String[] countryNames={" 91"," 86"," 61"," 351"," 1"," 64"};
int flags[] = {R.drawable.india, R.drawable.china, R.drawable.australia, R.drawable.portugal, R.drawable.america, R.drawable.new_zealand};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Getting the instance of Spinner and applying OnItemSelectedListener on it
Spinner spin = (Spinner) findViewById(R.id.simpleSpinner);
spin.setOnItemSelectedListener(this);
CustomAdapter customAdapter=new CustomAdapter(getApplicationContext(),flags,countryNames);
spin.setAdapter(customAdapter);
}
//Performing action onItemSelected and onNothing selected
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {
//Toast.makeText(getApplicationContext(), countryNames[position], Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
CustomAdapter.java:
public class CustomAdapter extends BaseAdapter {
Context context;
int flags[];
String[] countryNames;
LayoutInflater inflter;
public CustomAdapter(Context applicationContext, int[] flags, String[] countryNames) {
this.context = applicationContext;
this.flags = flags;
this.countryNames = countryNames;
inflter = (LayoutInflater.from(applicationContext));
}
@Override
public int getCount() {
return flags.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.custom_spinner_items, null);
ImageView icon = (ImageView) view.findViewById(R.id.imageView);
TextView names = (TextView) view.findViewById(R.id.textView);
icon.setImageResource(flags[i]);
names.setText(countryNames[i]);
return view;
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@ id/card_view"
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="200dp"
card_view:cardCornerRadius="4dp">
<Spinner
android:id="@ id/simpleSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello World!"
/>
</androidx.cardview.widget.CardView>
</LinearLayout>
custom_spinner_items.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="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@ id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp"
android:src="@drawable/ic_launcher" /><!--Make sure image is present in Drawable folder-->
<TextView
android:id="@ id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="5dp"
android:text="Enter phone number"
android:textColor="#000" />
</LinearLayout>
Комментарии:
1. Вы должны использовать a
ConstraintLayout
, это более эффективно, чем наличие нескольких иерархий внутриLinearLayout
.
Ответ №1:
Это произошло из-за того, что вы поместили представление карты внутри линейного отображения, а не содержимое представления карты . Вам нужно создать линейную разметку внутри представления карты, а затем назначить ее горизонтальной свойству ориентации для удовлетворения ваших потребностей .
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@ id/card_view"
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="200dp"
card_view:cardCornerRadius="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Spinner
android:id="@ id/simpleSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello World!"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>