Android - 如何将Horizo​​ntalScrollView元素的宽度与屏幕宽度相匹配?

时间:2018-01-12 11:52:39

标签: android

我有一个Horizo​​ntalScrollView,里面有TextViews。我想将TextView的宽度与屏幕的宽度相匹配。在尝试时,我得到以下

Each TextView does not match it's width to that of the screen

我尝试使用android:fillViewport。它帮助TextView将其宽度与屏幕的宽度相匹配,但无法滚动这样做。

这是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="com.example.Mahesh.practice.test">

    <HorizontalScrollView
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:background="@color/background"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintVertical_bias="0.0">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/colorAccent"
                android:text="Film"
                android:textAlignment="center" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/test"
                android:text="Film"
                android:textAlignment="center" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/colorPrimary"
                android:text="Film"
                android:textAlignment="center" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/colorAccent"
                android:text="Film"
                android:textAlignment="center" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/test"
                android:text="Film"
                android:textAlignment="center" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/colorPrimary"
                android:text="Film"
                android:textAlignment="center" />
        </LinearLayout>
    </HorizontalScrollView>
</android.support.constraint.ConstraintLayout>


我如何将TextView的宽度与屏幕的宽度相匹配,并允许ScrollView滚动?为什么屏幕如上图所示?

1 个答案:

答案 0 :(得分:0)

通过计算屏幕宽度以编程方式设置textview的宽度。

试试这个

activity_main.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="com.example.Mahesh.practice.test">
<HorizontalScrollView
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    android:background="@color/background"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintVertical_bias="0.0">
    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorAccent"
            android:text="Film"
            android:textAlignment="center" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/test"
            android:text="Film"
            android:textAlignment="center" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorPrimary"
            android:text="Film"
            android:textAlignment="center" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorAccent"
            android:text="Film"
            android:textAlignment="center" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/test"
            android:text="Film"
            android:textAlignment="center" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorPrimary"
            android:text="Film"
            android:textAlignment="center" />
    </LinearLayout>
</HorizontalScrollView>

MainActivity.java

public class MainActivity extends AppCompatActivity {
private LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    linearLayout=findViewById(R.id.ll);
    int width=getScreenWidth(Main2Activity.this);

    int childCount=linearLayout.getChildCount();
    for (int i=0;i<childCount;i++){
        TextView textView= (TextView) linearLayout.getChildAt(i);
        textView.setWidth(width);
    }
}
public static int getScreenWidth(Context context) {
    WindowManager windowManager = (WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics dm = new DisplayMetrics();
    windowManager.getDefaultDisplay().getMetrics(dm);
    return dm.widthPixels;
}
}