如何将ListView按钮拉伸到整个屏幕宽度

时间:2018-07-30 06:56:09

标签: android android-layout listview

我的activity_main.xml:

<ListView
    android:id="@+id/MyListItem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    tools:layout_editor_absoluteY="8dp" />

我的mainactivity.java:

public class MainActivity extends AppCompatActivity {
    private String[] mMonthArray = { "Январь", "Февраль", "Котомарт", "Апрель", "Май",
            "Июнь", "Июль", "Август", "Сентябрь", "Октябрь", "Ноябрь",
            "Декабрь" };

    private ArrayAdapter<String> mMonthAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView myListView = findViewById(R.id.MyListItem);
        mMonthAdapter = new ArrayAdapter<>(this,
                android.R.layout.simple_list_item_1, mMonthArray);
        myListView.setAdapter(mMonthAdapter);

    }
}

我在ListView中的按钮没有像ListView那样全宽:

enter image description here

1 个答案:

答案 0 :(得分:1)

您的ListView包装到内容宽度。尝试将宽度更改为match_parent,如下所示。

<ListView
android:id="@+id/MyListItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
tools:layout_editor_absoluteY="8dp" />