Horizo​​ntalScrollView

时间:2015-09-01 09:20:50

标签: android android-layout horizontalscrollview

我试图膨胀&将RelativeLayout - s添加到HorizontalScrollViewRelativeLayout(thumbnail.xml)有一个TextView作为后代,问题是TextView没有显示;没有任何其他观点。

MainActivity.java:

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LayoutInflater layoutInflater = LayoutInflater.from(this);
        LinearLayout contents = (LinearLayout)findViewById(R.id.contents);

        //LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        // changed to WRAP_CONTENT but doesn't seem to make a difference
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        RelativeLayout thumbnailLayout = (RelativeLayout)layoutInflater.inflate(R.layout.thumbnail, null);
        contents.addView(thumbnailLayout, layoutParams);
    }
}

activity_main.xml中:

<HorizontalScrollView 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"
    tools:context=".MainActivity">

    <!-- ① -->
    <LinearLayout
        android:id="@+id/contents"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="horizontal" />

</HorizontalScrollView>

thumbnail.xml:

<?xml version="1.0" encoding="utf-8"?>
<!-- ② -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <!-- BEWARE: You MUST resize the drawable 'null_thumbnail' when thumbnails get resized! (2015.08.31) -->
    <!-- ④ -->
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:contentDescription="This ImageView only exists in order to fix the ratio of the whole layout."
        android:src="@drawable/null_thumbnail"
        tools:ignore="HardcodedText" />

    <!-- IF YOU REMOVE THE BACKGROUND ATTRIBUTE, THE TEXTVIEW DISAPPEARS!? -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/transparent"
        android:orientation="vertical"
        android:weightSum="128">

        <!-- dummy -->  
        <View
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="4" />

        <!-- ③ -->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="27"
            android:text="Can you see this?"
            android:textColor="#FF0000"
            android:textSize="50sp" />
    </LinearLayout>
</RelativeLayout>

我发现添加任何drawable作为背景解决了问题,但我仍然感到困惑。是设计还是我错了?

我尝试使用R.layout.thumbnailLinearLayout重构ScrollView的根,但唯一重要的是HorizontalScrollView

更新

enter image description here

(注意,体系结构实际上并未在单个.xml文件中定义。)

我想要显示具有相同尺寸( WIDTH × HEIGHT )的动态图像作为RelativeLayout(②)和TextView(③)的背景。 ②是LinearLayout的子元素,①,它是Horizo​​ntalScrollView的唯一子元素。

为了保持每个②的宽高比,我在④中放置了一个 WIDTH × HEIGHT 透明drawable(null_thumbnail),设置大小②包含④的内容。

Phew,希望这是有道理的。

1 个答案:

答案 0 :(得分:0)

RelativeLayout中的ImageView和LinearLayout都将布局宽度和高度设置为<?xml version="1.0" encoding="UTF-8"?> <st:Statistics xmlns="http://schema.ebinterface.at/schema/4p1/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ebinterface.at/schema/4p1/" st:Type="abc" st:Title="Statistics"> <st:ABC st:dsig="fh">Sample ABC</st:ABC> <st:DEFG> <st:Note>Wir ersuchen um termingerechte Bezahlung.</st:Note> <st:Amount st:currencyCode="EUR">12.36</st:Amount> <st:Test1/> </st:DEFG> </stStatistics>

match_parent

当RelativeLayout的布局宽度和高度设置为android:layout_width="match_parent" android:layout_height="match_parent" 时,这会产生一种相互依赖性,这会导致像你遇到的那样奇怪的行为。

修改 对我来说错了,你将程序中RelativeLayout的宽度和高度覆盖为 match_parent ,但是RelativeLayout的父级,LinearLayout wrap_content的宽度是 wrap_content ,这个是实际相互依赖导致问题的地方。

需要了解最终产品的预期外观,以便实际建议您应如何纠正它。