Содержимое центра в виде прокрутки
Я хочу центрировать мой LinearLayout в ScrollView. Когда высота LinearLayout мала, она центрируется в порядке (см. Изображение # 1), но когда высота LinearLayout больше высоты экрана, это ведет себя странно. Я не вижу вершины LinearLayout (см. Изображение # 2), а внизу ScrollView - огромное дополнение. Я не знаю, что здесь происходит. Когда в LinearLayout имеется много контента, весь экран должен выглядеть как на рисунке # 3.
изображение # 1
изображение # 2
изображение # 3
Здесь мой файл макета:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cccfff" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="28dp"
android:background="#ffffff"
android:orientation="vertical"
android:paddingBottom="40dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="40dp" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/tip_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:text="Title"
android:textColor="@color/orange_text"
android:textSize="@dimen/font_size_medium" />
<TextView
android:id="@+id/tip_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Description description..."
android:textSize="@dimen/font_size_small" />
</LinearLayout>
</ScrollView>
Ответы
Ответ 1
Вы должны использовать внешний горизонтальный LinearLayout. Это работает для меня в той же ситуации.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<TextView
android:text="@string/your_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Ответ 2
Вы можете использовать android:fillViewport="true"
для центра содержимого. Что-то вроде этого:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<!-- content -->
</ScrollView>
Ответ 3
Установите фокус на свой самый верхний компонент пользовательского интерфейса, который вы хотите видеть
<ImageView
android:focusable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:src="@drawable/ic_launcher" />
Ответ 4
Попробуйте, это может удовлетворить ваши требования.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cccfff"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:background="#ffffff"
android:gravity="center_vertical"
android:orientation="vertical"
android:paddingBottom="40dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="40dp" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/tip_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:text="Title" />
<TextView
android:id="@+id/tip_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="biruDescription description..." />
</LinearLayout>
</LinearLayout>
Еще одна строка в вашем Java-коде
TextView tip_description = (TextView) findViewById(R.id.tip_description);
tip_description.setMovementMethod(new ScrollingMovementMethod());
Это позволит прокрутить содержимое вашего описания довольно целым.
Ответ 5
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="10dp"
android:background="#cccfff">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="28dp"
android:background="#ffffff"
android:orientation="vertical"
android:paddingBottom="40dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="40dp" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/tip_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:text="Title"
android:textColor="@color/orange_text"
android:textSize="@dimen/font_size_medium" />
<TextView
android:id="@+id/tip_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Description description..."
android:textSize="@dimen/font_size_small" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Ответ 6
Просто используйте мой класс CenteringLinearLayout ниже.
import android.content.Context
import android.support.constraint.ConstraintLayout
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
class CenteringLinearLayout: LinearLayout {
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
constructor(context: Context) : super(context)
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
val parentView = this.parent as View
if (h > parentView.height) {
parentView.post {
val params = parentView.layoutParams as ConstraintLayout.LayoutParams
params.height = ConstraintLayout.LayoutParams.MATCH_CONSTRAINT
parentView.layoutParams = params
}
} else {
val params = parentView.layoutParams
params.height = ConstraintLayout.LayoutParams.WRAP_CONTENT
parentView.layoutParams = params
}
super.onSizeChanged(w, h, oldw, oldh)
}
}
Ответ 7
Попробуйте поместить маржу и paddings в ScrollView
Ответ 8
Может быть, неясно, как работает макет. В вашем случае вам нужно установить внешний макет, который заполняет родительский элемент и имеет гравитацию, заданную как центр, внутри есть вид прокрутки с высотой макета wrap_content и внутренний макет с высотой макета wrap_content. Затем внешняя компоновка будет центрировать прокрутку, если она меньше.
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<ScrollView
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Content here -->
</LinearLayout>
</ScrollView>
</LinearLayout>