Android Studio将底部边距追加到listView

时间:2019-07-17 08:42:30

标签: android listview margin

我使用ConstraintLayout在片段中有一个列表视图。这里的xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:wheel="http://schemas.android.com/apk/res-auto"
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/appTintColor"
tools:context=".ContactsFragment">

<android.support.constraint.ConstraintLayout
    android:id="@+id/contactsNavigationBar"
    android:layout_width="0dp"
    android:layout_height="40dp"
    android:background="@color/appMainColor"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <Button
        android:id="@+id/settingsButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginBottom="8dp"
        android:background="@drawable/settings_icon"
        android:textColor="@color/appTintColor"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/numberTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:text="@string/hello_blank_fragment"
        android:textColor="@color/appTintColor"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

<TextView
    android:id="@+id/textView2"
    android:layout_width="0dp"
    android:layout_height="20dp"
    android:layout_marginTop="1dp"
    android:background="@color/appMainColor"
    android:paddingStart="8dp"
    android:paddingLeft="8dp"
    android:text="@string/contacts"
    android:textColor="@color/appTintColor"
    app:layout_constraintBottom_toTopOf="@+id/contactsListView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/contactsNavigationBar" />

<ListView
    android:id="@+id/contactsListView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:divider="@color/appMainColor"
    android:dividerHeight="1px"
    android:footerDividersEnabled="false"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView2"/>

<com.pnikosis.materialishprogress.ProgressWheel
    android:id="@+id/contactsProcessingWheel"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="#80FFFFFF"
    android:clickable="true"
    android:elevation="1000000dp"
    android:focusable="true"
    android:focusableInTouchMode="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView2"
    wheel:matProg_barColor="@color/appTintColor"
    wheel:matProg_barWidth="4dp"
    wheel:matProg_circleRadius="50dp"
    wheel:matProg_rimColor="@color/appMainColor"
    wheel:matProg_rimWidth="10dp" />

</android.support.constraint.ConstraintLayout>

如您所见,列表视图限制在父级的底部,并且高度设置为0dp,即match_constraints。但是当我运行应用程序时,ListView的底部空白。在这里看起来像什么。

enter image description here

为什么会发生这种情况以及如何解决?

注意。这是我的活动代码。

public class AuthenticatedActivity extends AppCompatActivity implements ContactsFragment.ContactsFragmentInteractionListener, SettingsFragment.SettingsFragmentInteractionListener {
private SharedPreferences sharedPreferences;

private Fragment fragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    sharedPreferences = getSharedPreferences(SP_fileName, MODE_PRIVATE);

    requestWindowFeature(Window.FEATURE_NO_TITLE); //will hide the title
    getSupportActionBar().hide(); // hide the title bar

    setContentView(R.layout.activity_authenticated);

    fragment = getSupportFragmentManager().findFragmentById(R.id.authenticatedfragment);
}

@Override
public void settingsClickedFromContacts() {
    cleanFragment();
    NavHostFragment.findNavController(fragment).navigate(R.id.action_contactsFragment_to_settingsFragment);
}

@Override
public void logOutClicked() {
    logOut.sendRequest(this);
    sharedPreferences.edit().remove(SP_tokenKey).remove(SP_numberKey).apply();
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
    finish();
}

@Override
public void backClicked() {
    NavHostFragment.findNavController(fragment).navigateUp();
}

private void cleanFragment() {
    ((ViewGroup) fragment.getView()).removeAllViews();
}

编辑附加我活动的xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AuthenticatedActivity">

<fragment
    android:id="@+id/authenticatedfragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:defaultNavHost="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:navGraph="@navigation/authenticated_graph" />
</android.support.constraint.ConstraintLayout>

0 个答案:

没有答案