约束布局有底部和顶部空间

问题描述 投票:1回答:1

我正在使用Constraint布局构建一个应用程序,但它的底部和顶部有额外的空间。我找不到它们是从哪里来的。我检查了大概十次从上到下的所有代码,但没有任何margin或padding或其他任何东西给它。

enter image description here

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black"
    android:paddingLeft="0dp"
    android:paddingTop="0dp"
    android:paddingRight="0dp"
    android:paddingBottom="0dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/Brand"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@id/guideline"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="horizontal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:id="@+id/brandname"
                style="@style/viewParent.headerText"
                android:text="Did"
                android:textColor="@color/white" />

            <TextView
                android:id="@+id/brandname2"
                style="@style/viewParent.headerText2"
                android:layout_toRightOf="@id/brandname"
                android:text="You"
                android:textColor="@color/yellow" />

            <TextView
                style="@style/viewParent.headerText2"
                android:layout_toRightOf="@id/brandname2"
                android:text="Know!"
                android:textColor="@color/red"
                android:textStyle="bold|italic" />
        </RelativeLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/container_main"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="4dp"
        android:background="@drawable/card_bg"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        app:layout_constraintBottom_toTopOf="@id/guideline2"
        app:layout_constraintEnd_toEndOf="@id/guidelineyazisag"
        app:layout_constraintStart_toStartOf="@id/guidelineyazisol"
        app:layout_constraintTop_toBottomOf="@id/guideline">

        <TextView
            android:id="@+id/activity_main_text_view_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="simple Title Text"
            android:textColor="#5E4C4C"
            android:textStyle="bold"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/activity_main_did_u_know"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="Category"
            android:textColor="#5E4C4C"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.core.widget.NestedScrollView
            android:id="@+id/scrollview"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:fillViewport="true"
            app:layout_constraintBottom_toTopOf="@id/activity_main_image_view"
            app:layout_constraintTop_toBottomOf="@id/activity_main_did_u_know">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                app:layout_constraintBottom_toTopOf="@id/activity_main_image_view"
                app:layout_constraintTop_toBottomOf="@id/activity_main_did_u_know">

                <TextView
                    android:id="@+id/factTextView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="8dp"
                    android:gravity="center_vertical|center_horizontal"
                    android:lineSpacingExtra="8dp"
                    android:padding="10dp"
                    android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged."
                    android:textColor="#000000"
                    android:textSize="16dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="1.0"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintVertical_bias="0.0" />

            </androidx.constraintlayout.widget.ConstraintLayout>

        </androidx.core.widget.NestedScrollView>

        <ImageView
            android:id="@+id/activity_main_image_view"
            android:layout_width="30dp"
            android:layout_height="40dp"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:clickable="true"
            android:src="@drawable/ic_share_black_24dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/scrollview" />

        <ImageView
            android:id="@+id/activity_main_fav_button"
            android:layout_width="30dp"
            android:layout_height="40dp"
            android:layout_margin="16dp"
            android:clickable="true"
            android:src="@drawable/ic_favorite_border_black_24dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@id/scrollview" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/LeftRight"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@id/adView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/guideline2">

        <ImageView
            android:id="@+id/activity_main_left_button"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:clickable="true"
            android:src="@drawable/ic_previous"
            android:elevation="3dp"
            android:background="@drawable/okarka"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@id/guideline3"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/activity_main_right_button"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:clickable="true"
            android:src="@drawable/ic_next"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            android:background="@drawable/okarka"
            app:layout_constraintStart_toEndOf="@+id/guideline3"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.5" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
        ads:layout_constraintBottom_toBottomOf="parent"
        ads:layout_constraintLeft_toLeftOf="parent"
        ads:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/LeftRight" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.076" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.7" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guidelineyazisol"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.07" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guidelineyazisag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.93" />
</androidx.constraintlayout.widget.ConstraintLayout>

我试着在底部添加准则到0点,但它也是从那个空间开始的。我给margin和padding加了-(减)值,但是没有用。我不知道还能做什么。你可以从截图中看到它的样子。

public class MainActivity extends AppCompatActivity {

    private ColorWheel colorWheel = new ColorWheel();
    private TextView factTextView;
    private TextView textViewId;
    private TextView textViewTableName;
    ImageView tableImageView;
    AdView adView;
    ImageView shareImageView;
    ImageView favImageView;
    float x1,x2,y1,y2;
    int i = 1;
    int adCounter = 0;
    private ConstraintLayout constraintLayout;
    String fact;
    ArrayList<Fact> favList = new ArrayList<>();
    private InterstitialAd mInterstitial;
    AdRequest interAdRequest;
    int favId;
    String favTable;
    Typeface typeface;
    List<Fact> mData;
    private DatabaseFacts db;
    GradientDrawable shape;

    @SuppressLint({"ClickableViewAccessibility", "SetTextI18n"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        db = new DatabaseFacts(this);
        db.copyDbIfNotExists();
        shape =  new GradientDrawable();
        shape.setCornerRadius(40);
        mData = new ArrayList<>();
        MobileAds.initialize(this, "ca-app-pub-2469721886989416~7390658870");
        adView = findViewById(R.id.adView);
        AdRequest bannerAdRequest = new AdRequest.Builder().build();
        adView.loadAd(bannerAdRequest);

        interAdRequest = new AdRequest.Builder().build();
        mInterstitial = new InterstitialAd(this);
        mInterstitial.setAdUnitId("ca-app-pub-2469721886989416/9441000894");
        mInterstitial.loadAd(interAdRequest);

        factTextView = findViewById(R.id.factTextView);
        constraintLayout = (ConstraintLayout) findViewById(R.id.container_main);
        textViewId = findViewById(R.id.activity_main_text_view_id);
        favImageView = findViewById(R.id.activity_main_fav_button);
        shareImageView = findViewById(R.id.activity_main_image_view);
        textViewTableName = findViewById(R.id.activity_main_did_u_know);
//        tableImageView = findViewById(R.id.img_user);
//        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
//            typeface = getResources().getFont(R.font.sourceserifproregular);
//        }
        factTextView.setTypeface(typeface);
        Intent intent  = getIntent();
        final int choice = intent.getIntExtra("choice",0);

        switch (choice) {
            case 1:

                textViewTableName.setText("General Facts");
                factTextView.setText(db.getFact((readFromShared("defaultKey") - 1), DatabaseFacts.TABLE_GENERAL_NAME).getFact());
                textViewId.setText("Fact " + (readFromShared("defaultKey") - 1) + " of " + db.getFactsCount(DatabaseFacts.TABLE_GENERAL_NAME));

                if (db.getFact((readFromShared("defaultKey") - 1), DatabaseFacts.TABLE_GENERAL_NAME).isFavorite() == 0) {
                    favImageView.setImageResource(R.drawable.ic_favorite_border_black_24dp);
                } else {
                    favImageView.setImageResource(R.drawable.ic_favorite_black_24dp);
                }

                ImageView leftClick = (ImageView) findViewById(R.id.activity_main_left_button);
                leftClick.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        leftClickMethod("defaultKey", DatabaseFacts.TABLE_GENERAL_NAME);

                    }
                });

                ImageView rightClick = (ImageView) findViewById(R.id.activity_main_right_button);
                rightClick.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        rightClickMethod("defaultKey",DatabaseFacts.TABLE_GENERAL_NAME);
                        return;
                    }
                });

                favImageView.setOnClickListener(v -> handleFavorites("defaultKey", DatabaseFacts.TABLE_GENERAL_NAME));
                break;
java android android-layout android-constraintlayout android-relativelayout
1个回答
2
投票

移除 app:layout_constraintBottom_toTopOf="@id/guideline"Brandapp:layout_constraintTop_toBottomOf="@id/LeftRight"adView. 你不能为顶部和底部设置限制,因为它会使你的中心位置 View 这两个约束之间。左右(开始,结束)也是一样的,但是你有 match_parent 这里,所以这个问题不会发生(直观地看--如果你在这里设置了 wrap_content,视图也会走到中心)。)

© www.soinside.com 2019 - 2024. All rights reserved.