本文介绍了LinearLayout中离开了太多的空白。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个Android应用程序,可以帮助小孩子学数学。它为用户提供了一些问题,用户将回答这些问题。如果他/她正确回答所有的人,奖品将给予。现在我需要告诉用户这个在 ResultsActivity 。这里是如何看起来像:

I am writing an Android app that helps little kids learn maths. It gives the user some questions and the user would answer them. If he/she answers all of them correctly, a prize would be given. Now I need to tell the user about this in the ResultsActivity. Here is how it looks like:

<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>

正如可以看到,在屏幕的左侧用于显示得分。右侧(即的LinearLayout id为 right_side )是空的,因为这将有它取决于是否在不同的事情用户正确回答所有的问题。如果他/她没有,它会显示哪些问题(S)做他/她没有错。这意味着我需要动态地添加欣赏到的LinearLayout 。试了几次后,我能够显示用户获得的奖金以及一些文本,告诉他/她。但是,有很多文字和<$​​ C $ C>的ImageView 之间的空白!这里是一个屏幕截图:

As you can see, the left side of the screen is used to display the score. The right side (The LinearLayout with the id right_side) is empty because it would have different things in it depending on whether the user answers all the questions correctly. If he/she doesn't, it would display which question(s) did he/she did wrongly. That means I need to add views dynamically to the LinearLayout. After a few tries, I am able to display the prize that the user gets and some text telling him/her. However, there are lots of empty between the text and the ImageView! Here is a screen shot:

希望你不介意,这是中国人。正如你所看到的,有很多的明星和文本的末端之间的空间。 (我甚至需要向下滚动一点看明星!)现在,这里是我的code以添加的看法:

Hope you don't mind that this is Chinese. As you can see, there are lots of space between the tip of the star and the text. (I even need to scroll down a little to see the star!) Now here is my code to add the views:

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_CONTENT match_parent 的东西倍。而每一次它不工作。我甚至试过权重增加的意见!我敢肯定的形象是一个完美的正方形,以便空白不是由图像引起的。

I tired a lot of times by changing those wrap_content and match_parent stuff. And every time it didn't work. I even tried to add weights to the views! And I'm sure that the image is a perfect square so the white space isn't caused by the image.

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

Just for clarity, here is the dimens.xml and 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>

你能告诉我为什么发生这种情况,我该如何解决这个问题?

Can you tell me why this is happening and how can I fix this?

推荐答案

如果该ImageView的比其母公司大,使用imageView.setAdjustViewBounds(真),以保持它的大小比。

If the imageView is larger than its parent, use imageView.setAdjustViewBounds(true) to keep its size ratio.

您可以看到附件中的图片之间的差异。

You can see the differences between the attached images.

    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);

这篇关于LinearLayout中离开了太多的空白。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 13:37