【问题标题】:ListView SetAdapter() Layout issueListView SetAdapter() 布局问题
【发布时间】:2016-03-07 21:12:13
【问题描述】:

这就是问题所在。 我有一个列表视图和一堆数组字符串。我想用每个字符串的一个文本视图填充列表视图。字符串被注入,但注入后布局与我在tip_row.xml 中定义的布局不同(特别是我看不到任何边距和高度)

代码如下:

TipsFragment.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:elevationandroid:layout_margin,但这两个属性不是由我的应用程序呈现的。

你可以在这里找到截图:Imgur

PS:我可以成功更改字体大小或填充,唯一不起作用的是该行项目的高度和边距。海拔在tips_header.xml 中也不起作用

谢谢

【问题讨论】:

  • 海拔字段仅适用于较新的 Android 版本,我相信 Lollipop 及更高版本。
  • 目前我正在使用 API 23 进行开发,并且正在使用 android 5.1 进行调试

标签: android xml android-layout listview android-inflate


【解决方案1】:

试试这个。将父 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 Pollard answer

希望对您有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-17
    相关资源
    最近更新 更多