【问题标题】:Button not appearing if layout expands too large如果布局展开太大,按钮不会出现
【发布时间】:2017-04-09 18:33:38
【问题描述】:

我有一个我认为是用于活动的简单线性布局:

<LinearLayout 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.mille.shithead.firstPlay"
android:orientation="vertical">

<TextView
    android:id="@+id/fp_TextView_mainText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAlignment="center"
    android:textStyle="bold"
    android:fontFamily="cursive"
    android:textSize="30sp"
    android:visibility="invisible"/>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">
    <LinearLayout
        android:id="@+id/fp_SV_placeHolder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="1">

    </LinearLayout>

</ScrollView>
<Button
    android:id="@+id/fp_nextButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal|center_vertical"
    android:layout_weight="1"
    android:textAlignment="center"
    android:visibility="visible"
    android:text="Let's Play!"/>

在 fp_SV_placeholder 中,我根据当前游戏实例中的玩家数量,根据需要填充以下布局的多个副本:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_horizontal|center_vertical">
<TextView
    android:id="@+id/fp_TV_player"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAlignment="center"
    android:textStyle="bold"
    android:fontFamily="cursive"
    android:textSize="15sp"
    />
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_gravity="center_horizontal|center_vertical">
    <ImageView
        android:id="@+id/fp_IV_tableUp1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:gravity="center_horizontal|center_vertical"
        android:padding="4dp"
        android:scaleType="centerInside"
        android:src="@drawable/i_back" />

    <ImageView
        android:id="@+id/fp_IV_tableUp2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:gravity="center_horizontal|center_vertical"
        android:padding="4dp"
        android:scaleType="centerInside"
        android:src="@drawable/i_back" />

    <ImageView
        android:id="@+id/fp_IV_tableUp3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:gravity="center_horizontal|center_vertical"
        android:padding="4dp"
        android:scaleType="centerInside"
        android:src="@drawable/i_back" />
</LinearLayout>

这在我编写的代码中按预期工作,并且正确的显示与预期的正确的卡片图像一起出现。但是,如果我的游戏中有两个以上的玩家,则奇怪地缺少移动到下一个活动的按钮:

这是我为各种玩家的卡片充气并为按钮设置 OnClickListener 的代码:

String display = "Looks like " + players.get(firstPlayerIndex).getName() + " goes first!";
    if (!firstPlayer.isComputer()) {
        display = "Looks like You go first!";
    }
    textViews.get(0).setText(display);

    ViewGroup vg = (ViewGroup) findViewById(R.id.fp_SV_placeHolder);

    for (int i = 0; i < game.getPlayers().size(); i ++) {
        //Inflate Player display and find TV and IV's
        View v = getLayoutInflater().inflate(R.layout.fp_player_top_3, vg, false);
        TextView textView = (TextView) v.findViewById(R.id.fp_TV_player);
        ImageView iv_Card1 = (ImageView) v.findViewById(R.id.fp_IV_tableUp1);
        ImageView iv_Card2 = (ImageView) v.findViewById(R.id.fp_IV_tableUp2);
        ImageView iv_Card3 = (ImageView) v.findViewById(R.id.fp_IV_tableUp3);

        //Set TextView display
        String textViewDisplay = "Your Top 3 Cards";
        if(players.get(i).isComputer()) {
            textViewDisplay = players.get(i).getName();
        }
        textView.setText(textViewDisplay);
        //set ImageView Displays
        int resID1 = getResources().getIdentifier(players.get(i).getTableUp().get(0).getImageIDString(),"drawable",getPackageName());
        iv_Card1.setImageResource(resID1);
        int resID2 = getResources().getIdentifier(players.get(i).getTableUp().get(1).getImageIDString(),"drawable",getPackageName());
        iv_Card2.setImageResource(resID2);
        int resID3 = getResources().getIdentifier(players.get(i).getTableUp().get(2).getImageIDString(),"drawable",getPackageName());
        iv_Card3.setImageResource(resID3);

        if(i == firstPlayerIndex) {
            v.setBackgroundColor(Color.BLUE);
        }
        //attach to the root
        vg.addView(v);
    }
    nextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Class nextClass = playerTurn.class;
            if (firstPlayer.isComputer()) {
                nextClass = computerTurn.class;
            }
            Intent intent = new Intent(getApplicationContext(),nextClass);
            startActivity(intent);
        }
    });

只要按钮可见,按钮就会按预期工作,但无论有多少玩家同时在游戏中,它都必须可见。有什么建议?我尝试简单地移动 scrollView 内的按钮,然后应用程序在尝试加载页面时中断。

【问题讨论】:

  • 所以fp_nextButton 不显示?这就是问题所在?

标签: android android-linearlayout android-scrollview android-inflate playing-cards


【解决方案1】:

不要赋予滚动视图权重。它不适用于滚动视图。 ScrollView 忽略权重,并根据其中内容的高度取高度。 因此,要解决它,您需要将滚动视图保持在线性布局中,并赋予线性布局权重而不是滚动视图

`<LinearLayout
        android:id="@+id/fp_SV_placeHolder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:id="@+id/fp_SV_placeHolder"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical" />
        </ScrollView>
    </LinearLayout>`

【讨论】:

  • 这行得通,从技术上讲,无论玩家人数多少,屏幕底部的按钮都可以点击,但是按钮仍然无法正常显示……它是无限小之类的。通过从按钮中删除 weight 参数,它现在仍然可见且可点击。非常感谢!
  • 我没有看到你也给按钮赋予了权重,如果你给两者都赋予权重,它会尝试根据权重调整两个视图但是如果你只给一个视图赋予权重,那么我将为每个视图提供适当的高度,并在剩余的可用空间中自行调整。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多