Android Landscape mode cutting off my buttons on top
I am having an issue with my application. I recently added ScrollView as the Parent of LinearLayout. When flip my device orientation to Landscape, my buttons get cut off on the top. I can see the last button just fine with extra "black space" at the end of it when I scroll down, only the top gets cut off. I looked around and I am not sure why this is happening. Here are some pictures so you can understand how it looks.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center_vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center">
<Button
android:id="@+id/BeefButton"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_gravity="center"
android:background="@drawable/backgroundlayer"
android:drawableLeft="@drawable/beefbutton"
android:text="Beef"
android:textColor="#FFFFFF"
android:textSize="32px"
android:textColorHighlight="#FF6600"/>
<Button
android:id="@+id/PoultryButton"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_width="match_parent"
android:background="@drawable/backgroundlayer"
android:drawableLeft="@dr开发者_JAVA百科awable/chickenbutton"
android:text="Poultry"
android:textColor="#FFFFFF"
android:textSize="32px"
android:textColorHighlight="#FF6600"/>
<Button
android:id="@+id/FishButton"
android:layout_height="wrap_content"
android:background="@drawable/backgroundlayer"
android:drawableLeft="@drawable/fishbutton"
android:text="Fish"
android:textColor="#FFFFFF"
android:textSize="32px"
android:textColorHighlight="#FF6600"
android:layout_gravity="center"
android:layout_width="match_parent"/>
<Button
android:id="@+id/PorkButton"
android:layout_height="wrap_content"
android:background="@drawable/backgroundlayer"
android:drawableLeft="@drawable/porkbutton"
android:text="Pork"
android:textColor="#FFFFFF"
android:textSize="32px"
android:textColorHighlight="#FF6600"
android:layout_gravity="center"
android:layout_width="match_parent"/>
<Button
android:id="@+id/VegetablesButton"
android:layout_height="wrap_content"
android:background="@drawable/backgroundlayer"
android:drawableLeft="@drawable/vegetablesbutton"
android:text="Vegetables"
android:textColor="#FFFFFF"
android:textSize="32px"
android:textColorHighlight="#FF6600"
android:layout_gravity="center"
android:layout_width="match_parent"/>
</LinearLayout>
</ScrollView>
remove the android:layout_gravity="center"
on your linearlayout , when you change it to landscape mode the height of the scrollview is bigger than your screen size, so its centering the linearlayout in the ScrollView, although removing it may not be the solution, that is what is causing this,
you may consider revising your layout, it might make more sense to use a listview to be able to successfully support landscape
I know this is an old thread, but I spent two days on this problem. While removing the "layout_gravity" statement does help the cut off issue, the comment on not being able to center is also true. Ultimately what worked for me, which in my opinion is a hack, was to have the scrollview on top of a layout centered and TWO linearlayouts cascaded as children. Also the height and width had to be wrap content on the scrollview and linearlayouts. This is not the neatest solution, but I tried everything with no success, including setting the gravity in code with setlayoutparams if the objects were less than the screen size width. I hope this helps someone.
The hint (removing android:layout_gravity="center"
) that @slim said does work but with no central alignment.
My Solution:
I fixed it by wrapping the main long layout with a LinearLayout
with its gravity
set to center
and letting android:layout_gravity="center"
remain for the main layout:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center">
<!-- .... the main long layout ... -->
</LinearLayout>
</ScrollView>
You can use android:layout_gravitiy="center"
with android:fillViewport="true"
Example a center box without cut off in landscape (Kotlin) xml:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:layout_gravity="center">
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/layout"
android:layout_width="340dp"
android:layout_height="443dp">
<!-- .... box perfect center do something in it... -->
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
精彩评论