这就是问题所在。我有一个列表视图和一堆数组字符串。我想为每个字符串用一个文本视图填充列表视图。字符串被注入,但注入后布局不会重复我在tip_row.xml中定义的布局(特别是我看不到任何边距和提升)
这是代码:
提示片段.java
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
FragmentActivity superActivity = super.getActivity();
lLayout = (LinearLayout) inflater.inflate(R.layout.tips_fragment, container, false);
String[] general_tips = getResources().getStringArray(R.array.general_tips);
List<String> tips = new ArrayList<>(Arrays.asList(general_tips));
tv = (TextView)lLayout.findViewById(R.id.general_tip_header);
tv.setText("Advises");
lv = (ListView)lLayout.findViewById(R.id.general_tips_list_view);
ArrayAdapter<String> itemsAdapter = new ArrayAdapter<String>(superActivity, R.layout.tip_row, tips); //the layout is tip_row
lv.setAdapter(itemsAdapter);
String[] noobs_tips = getResources().getStringArray(R.array.noob_tips);
//tips.clear();
tips.addAll(Arrays.asList(noobs_tips));
String[] pro_tips = getResources().getStringArray(R.array.pro_tips);
//tips.clear();
tips.addAll(Arrays.asList(pro_tips));
//overrideFonts(superActivity, container);
return lLayout;
}
tips_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include android:id="@+id/general_tip_header"
layout="@layout/tips_header"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/general_tips_list_view"></ListView></LinearLayout>
tips_header.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:elevation="4dp"
android:gravity="center"
android:textSize="30sp"
android:text="General Problems" />
tip_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tip_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_margin="30dp"
android:textSize="20sp"
android:text="Test"
android:elevation="9dp"/>
请注意,在tip_row.xml中,我定义了android:elevation
和android:layout_margin
但这两个属性不是由我的应用程序呈现的。
在这里你可以找到一个截图:Imgur
PS:我可以成功更改字体大小或填充,唯一不起作用的是此行项目的提升和边距。提升在tips_header.xml中也不起作用
谢谢
试试这个。将父RelativeLayout
添加到TextView
元素,并将android:clipToPadding="false"
设置为父元素。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false">
<TextView
android:id="@+id/tip_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_margin="30dp"
android:textSize="20sp"
android:text="Test"
android:elevation="9dp"/>
</RelativeLayout>
欲了解更多信息@Justin请查看波拉德的答案
希望它有帮助!