Poner una vista de texto encima de un fragmento de lista
¿Cómo puedo añadir una vista de texto encima de un fragmento. Estoy utilizando el siguiente adaptador para rellenar el listfragment y el archivo xml es cada fila en el fragmento de lista. ¿Necesito agregar un textview ontop de listview que debe ser desplazable junto con la lista?
public class Adapter extends BaseAdapter { Context context; public TextView txtName; public TextView txtTitle; private LayoutInflater mInflater; private Storage storage; FontManager fontManager; Typeface typeface; public Adapter(Context _context,Storage _storage) { context = _context; mInflater = LayoutInflater.from(context); this.storage = _storage; fontManager = new FontManager(_context); typeface = fontManager.getTypeFace(); } @Override public int getCount() { // TODO Auto-generated method stub return _storage.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub convertView = mInflater.inflate(R.layout.row3, null); txtName = (TextView) convertView.findViewById(R.id.tvName); txtName.setTypeface(typeface); txtName.setText(storage.getName(position)); txtTitle = (TextView) convertView.findViewById(R.id.tvTitle); txtTitle.setTypeface(typeface); txtTitle.setText(storage.getTitle(position)); return convertView; } }
Xml:
- ¿Cómo agregar la imagen en un texto de TextView?
- ScrollView no muestra su contenido correctamente en versiones inferiores de android
- Cómo dar líneas de 5dp a TextView programaticamente
- TextView funciona mal con breakStrategy = "equilibrado" y layout_width = "wrap_content"
- Cómo asignar el tamaño de texto en el valor de sp usando código java
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="vertical" android:padding="20dp" android:scrollbars="none" android:id="@+id/ll1"> <TextView android:id="@+id/tvName" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" android:textColor="@android:color/black" android:textSize="20sp" android:textStyle="bold" /> <TextView android:id="@+id/tvTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" android:textColor="@android:color/black" /> </LinearLayout>
ListFragment:
public class SampleListFragment extends ListFragment { int number; @Override public void onActivityCreated(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onActivityCreated(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { number = getArguments().getInt( "num", 0); new SampleAsyncTask(this, getActivity(), number).execute(); return super.onCreateView(inflater, container, savedInstanceState); } @Override public void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub } } }
El siguiente código se utiliza en la clase AsyncTask para rellenar el fragmento de lista
Adapter adapter = new Adapter(context, storage); listFragment.setListAdapter(adapter);
- Cómo establecer un texto sobre una imagen?
- Margen dentro del texto de edición en android
- ¿Cómo comprobar si un EditText en Android tiene emoticon o no?
- Con quién comparar un fondo Recurso de imagen TextView con una imagen R.drawable.bg_image para cambiar
- ¿Hay una manera fácil de agregar un borde a la parte superior e inferior de una vista de Android?
- ImageSpan.ALIGN_BASELINE cuando TextView tiene lineSpacing
- ¿Cómo puedo poner un borde alrededor de una vista de texto de Android?
- TextView en el centro de la pantalla
Usted puede hacer como abajo o agregar una vista de texto como un encabezado a su listivew
Citando los documentos
ListActivity (ListFragment es similar) tiene un diseño predeterminado que consiste en una sola lista de pantalla completa en el centro de la pantalla. Sin embargo, si lo desea, puede personalizar el diseño de la pantalla estableciendo su propio diseño de vista con setContentView()
en onCreate () . To do this, your own view MUST contain a
. To do this, your own view MUST contain a
objeto ListView` con la identificación "@android: id / list" (o lista si está en código)
Activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <fragment android:name="com.example.listfragment.MyFragment" android:id="@+id/frag" android:layout_above="@+id/button1" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </RelativeLayout>
MyFragment.java
public class MyFragment extends ListFragment { String names[] ={"A","B","C"}; @Override public void onActivityCreated(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onActivityCreated(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View myFragmentView = inflater.inflate(R.layout.list_frag, container, false); TextView tv = (TextView) myFragmentView.findViewById(R.id.textView1); tv.setText("My Header"); setListAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,names)); return myFragmentView; } @Override public void onListItemClick(ListView l, View v, int position, long id) { // on click display the item in toast Toast.makeText(getActivity(), (String)l.getItemAtPosition(position), Toast.LENGTH_SHORT).show(); } }
List_frag.xml
<?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" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:text="TextView" /> <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" > </ListView> </RelativeLayout>
Tiro rápido
Editar: si desea que la vista de texto se desplace
<?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" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="19dp" android:text="TextView" /> </RelativeLayout>
Antes de llamar a setListAdapter
View view = inflater.inflate(R.layout.text, null); TextView textinlfated = (TextView) view.findViewById(R.id.textView1); ListView lv = getListView(); textinlfated.setText("TextView scrolls"); lv.addHeaderView(view);
Cambie el método getView del adaptador y coloque un caso en la posición y en caso de 0, devuelva su texto en otros casos.
- ¿Cómo puedo utilizar un CursorAdapter personalizado con AutoCompleteTextView?
- Crear un archivo en diferentes teléfonos móviles en Qt