我有一个片段,其中有一个ListView。 ListView使用一个简单的ArrayAdapter填充,其代码如下所示。 我希望ListView显示从RIGHT到LEFT的内容。 (我希望ListView的每一行都将其文本设置为右侧) 设定为右边的重力似乎无能为力。
我在这里尝试了几个解决方案,但我不知道为什么无论我做出什么改变我都没有看到任何差异。
我已将ListView
和Layouts
的宽度更改为match_parent
,fill_parent
,wrap_content
......没有任何变化。
我的活动xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context="com..SearchResultListActivity"
tools:ignore="MergeRootFrame"/>
My Fragment xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_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"
android:layout_gravity="right"
android:gravity="right"
android:orientation="vertical"
tools:context="com.***.SearchResultListActivity$PlaceholderFragment" >
<ListView
android:id="@+id/listViewSearchResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right">
</ListView>
</LinearLayout>
我的活动类:
public class SearchResultListActivity extends ActionBarActivity {
//LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS
//ArrayList<String> listItems=new ArrayList<String>();
//DEFINING A STRING ADAPTER WHICH WILL HANDLE THE DATA OF THE LISTVIEW
//ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_result_list);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.search_result_list, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
String[] array = new String[] {"cat", "dog", "mouse"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, array);
ListView listView = (ListView) getActivity().findViewById(R.id.listViewSearchResult);
listView.setAdapter(adapter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_search_result_list, container, false);
return rootView;
}
}
}
答案 0 :(得分:1)
您正在使用Android列表项布局,这就是您没有正确看到内容的原因,也因为您更改了ListView
本身的重力,而不是其内容。您应该设置项目的重力,并且必须使用自定义适配器在新的布局文件中声明它们。
首先从 Fragment xml 布局中删除android:layout_gravity="right"
,不再需要它了。
此外,ListView
宽度必须设置为match_parent
。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_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"
android:orientation="vertical"
tools:context="com.***.SearchResultListActivity$PlaceholderFragment" >
<ListView
android:id="@+id/listViewSearchResult"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
现在,您必须创建一个新布局来表示ListView
。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item_row"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:padding="10dp">
<TextView android:id="@+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:textStyle="bold"
android:textSize="22dp"
android:textColor="#000000"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
</LinearLayout>
正如您所看到的,我们在此布局中设置了项目的layout_gravity
,这样Listview
的内容将具有此行为,位于{{1}的右侧}。
此外,您必须创建自定义适配器以使布局膨胀并正确设置值。这就是您的Activity应该如何将自定义数组适配器视为内部类
ListView
现在,您必须根据之前创建的自定义适配器初始化适配器,如下所示。
public class SearchResultListActivity extends ActionBarActivity {
//LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS
//ArrayList<String> listItems=new ArrayList<String>();
//DEFINING A STRING ADAPTER WHICH WILL HANDLE THE DATA OF THE LISTVIEW
//ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_result_list);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.search_result_list, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
String[] array = new String[] {"cat", "dog", "mouse"};
// Here we initialize the custom array adapter sending the proper values to the constructor
MyArrayAdapter adapter = new MyArrayAdapter(getActivity(), R.layout.list_item, array);
ListView listView = (ListView) getActivity().findViewById(R.id.listViewSearchResult);
// Here we set the custom adapter to the Listview
listView.setAdapter(adapter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_search_result_list, container, false);
return rootView;
}
}
// This is the custom adapter class where you will manage the behavior of your ListView content
public class MyArrayAdapter extends MyArrayAdapter<String>{
Context context;
int layoutResourceId; // This is the layout you created for the list items
String data[] = null; // the array with the data to populate the listview
public MyArrayAdapter(Context context, int layoutResourceId, String[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
// Here we inflate the list item layout to load its views (the TextView)
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
// Now we can get the TextView from the layout
TextView txtTitle = (TextView)row.findViewById(R.id.txtTitle);
// Here we set the value from a specific position in the array
txtTitle.setText(data[position]);
return row;
}
}
}
正如您在此处所见,我们发送自定义MyArrayAdapter adapter = new MyArrayAdapter(getActivity(), R.layout.list_item, array);
布局和数组,其中包含您要填充的数据以及构造函数上的上下文。
现在,您必须将之前创建的适配器设置为list_item.xml
ListView
如果您有更多疑问,请告诉我。
希望它对你有所帮助。
答案 1 :(得分:0)
你已经改变了ListView
容器的重力,但没有改变它所包含的行,正如我所想的那样。
有些不清楚是否需要右对齐文本或从左到右填充的文本。无论哪种方式,解决方案都是一样的。
最佳解决方案是将代码从android.R.layout.simple_list_item_1
复制到您自己的布局中,并更改其中TextView
的属性。 (除非您想要更改更多的适配器代码,否则请确保将View的ID保持不变。)
您可以在<android-sdk-root>/platforms/<version>/data/res/layout/
找到内置布局。
然后使用以下命令加载适配器:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity(),
R.layout.my_simple_list_item, // use your layout
array);
v19 simple_list_item_1供参考:
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
/>