【问题标题】:LinearLayout leaving out too much white space. Why?LinearLayout 留下太多的空白。为什么?
【发布时间】:2015-08-28 00:34:38
【问题描述】:

我正在编写一个帮助小孩子学习数学的 Android 应用。它给用户一些问题,用户会回答。如果他/她都答对了,将获得奖品。现在我需要在ResultsActivity 中告诉用户这件事。下面是它的样子:

<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                android:orientation="horizontal"
                tools:context="com.smartkidslovemaths.ResultsActivity">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/left_side"
        android:orientation="vertical"
        android:gravity="center">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/score_label"
            android:textSize="@dimen/score_label_size"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="100"
            android:id="@+id/score_value"
            android:textSize="@dimen/score_size"/>
    </LinearLayout>

    <View
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:background="#FF0000FF" />

    <ScrollView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/right_side"
            android:layout_margin="@dimen/question_display_margin"
            android:orientation="vertical">

        </LinearLayout>
    </ScrollView>

</LinearLayout>

如您所见,屏幕左侧用于显示乐谱。右侧(ID 为right_sideLinearLayout)是空的,因为它会根据用户是否正确回答所有问题而包含不同的内容。如果他/她没有,它会显示他/她做错了哪些问题。这意味着我需要动态地将视图添加到LinearLayout。经过几次尝试,我能够显示用户获得的奖品和一些告诉他/她的文字。但是,文字和ImageView之间有很多空白!这是一个屏幕截图:

希望你不介意这是中文。如您所见,星形的尖端和文本之间有很多空间。 (我什至需要向下滚动一点才能看到星星!)现在这是我添加视图的代码:

private void displayPrize () { //This is called in the onCreate() method
    Resources res = getResources ();
    int marginAll = (int)res.getDimension (R.dimen.prize_display_margin);
    FrameLayout.LayoutParams parentParams = new FrameLayout.LayoutParams (FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
    parentParams.setMargins (marginAll, marginAll, marginAll, marginAll);
    ((LinearLayout)findViewById (R.id.right_side)).setLayoutParams (parentParams);

    TextView text = new TextView (this);
    final LinearLayout.LayoutParams textParams =
            new LinearLayout.LayoutParams (
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT
            );

    final LinearLayout.LayoutParams imageParams =
            new LinearLayout.LayoutParams (
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT
            );
    text.setLayoutParams (textParams);
    QuestionOptions options = QuestionsActivity.Options.getOptions ();

    switch (options.getDigitCount ()) {
        case 1:
        default:
            text.setText (res.getText (R.string.earn_star_text));
            break;
        case 2:
            text.setText (res.getText (R.string.earn_badge_text));
            break;
        case 3:
            text.setText (res.getText (R.string.earn_trophy_text));
            break;
    }

    text.setTextSize (res.getDimension (R.dimen.answer_text_size) / 1.5F);

    ((LinearLayout)findViewById (R.id.right_side)).addView (text);

    ImageView image = new ImageView (this);
    image.setLayoutParams (imageParams);
    int resID = QuestionOptionMaps.getOptionsDrawableMap ().get (options);
    image.setImageResource (resID);
    ((LinearLayout)findViewById (R.id.right_side)).addView (image);
}

通过更改 wrap_contentmatch_parent 的东西,我已经厌倦了很多次。而且每次都不行。我什至尝试为视图添加权重!而且我确信图像是一个完美的正方形,因此空白不是由图像引起的。

为了清楚起见,这里是dimens.xml 和strings.xml

<resources>
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
    <dimen name="question_margin">25dp</dimen>

    <dimen name="customize_menu_title">15pt</dimen>
    <dimen name="customize_menu_radio">13pt</dimen>
    <dimen name="question_text_size">20pt</dimen>
    <dimen name="answer_text_size">12pt</dimen>

    <dimen name="radio_button_spacing">20dp</dimen>
    <dimen name="item_spacing">20dp</dimen>

    <dimen name="score_label_size">20pt</dimen>
    <dimen name="score_size">40pt</dimen>
    <dimen name="question_display_margin">4dp</dimen>
    <dimen name="prize_display_margin">32dp</dimen>
</resources>

<resources>
    <string name="app_name">Smart Kids Love Maths</string>

    <string name="button_trophies">My Prizes</string>
    <string name="button_start">Start!</string>

    <string name="digit_text">Number of digits</string>
    <string name="operator_text">Operation</string>
    <string name="timer_enabled_text">Timer Enabled</string>
    <string name="title_activity_questions">Smart Kids Love Maths</string>

    <string name="title_activity_results">Results</string>
    <string name="score_label">Your Score:</string>

    <string name="correct_text">Correct!</string>
    <string name="wrong_text">Wrong!</string>
    <string name="correct_answer_text">Correct Answer: </string>
    <string name="earn_trophy_text">You earned a new trophy!</string>
    <string name="earn_badge_text">You earned a new badge!</string>
    <string name="earn_star_text">You earned a new star!</string>
</resources>

你能告诉我为什么会这样吗?我该如何解决这个问题?

【问题讨论】:

  • 您是否尝试过在两个维度上都将imageParams 更改为WRAP_CONTENT
  • @PauloAvelar 因为图片有点大,我想这会放大到实际大小,对吧?
  • @PauloAvelar 现在我试了一下,还是空白
  • 是的,会的。我建议您稍后在项目中调整它的大小。是否有任何理由不能将其添加到您的 XML 布局中 invisible 然后在显示之前设置文本和图像源?这将帮助您设计和预览最终布局。
  • 不是LinearLayout的问题。您需要为大 ImageView 调用“setAdjustViewBounds(true)”。

标签: java android android-layout android-xml


【解决方案1】:

如果 imageView 大于其父级,则使用 imageView.setAdjustViewBounds(true) 保持其大小比例。

您可以看到附加图像之间的差异。

    LinearLayout linear = (LinearLayout) findViewById(R.id.left);
    TextView textView = new TextView(this);
    textView.setText("Test Right 1");
    textView.setTextSize(32);
    LinearLayout.LayoutParams tParams = new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    linear.addView(textView, tParams);

    ImageView imageView = new ImageView(this);
    imageView.setImageResource(R.mipmap.img);
    imageView.setAdjustViewBounds(true); // This is the trick
    LinearLayout.LayoutParams iParams = new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    linear.addView(imageView, iParams);

【讨论】:

    猜你喜欢
    • 2021-07-14
    • 2021-12-25
    • 1970-01-01
    • 2019-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-03
    • 1970-01-01
    相关资源
    最近更新 更多