0

我正在创建一个选项卡式 Android 应用程序。在 Fragment 中,我想要一个带有 TableLayout 的 ScrollView。但它不起作用。我按下按钮,但没有显示任何内容。我会发布所有内容:

片段 XML

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:layout_weight="1"
    android:background="@color/egg_shell" >

    <ScrollView
        android:id="@+id/playersScrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_span="2"
        android:padding="5dp" >

        <TableLayout
            android:id="@+id/playerTableScrollView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:stretchColumns="yes" >
        </TableLayout>
    </ScrollView>

</TableRow>

<TableRow
    android:id="@+id/tableRow2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/addButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/morePlayer"
        android:layout_span="2" 
        android:layout_weight="1" />
    </TableRow>

</TableLayout>

表格布局的行

<EditText
    android:id="@+id/playerEditText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:stretchColumns="yes"
    android:inputType="text" >

</EditText>

<ImageButton
    android:id="@+id/deletePlayerImageButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@string/deletePlayerImageButton"
    android:src="@android:drawable/ic_menu_delete" />

</TableRow>

片段java

import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ScrollView;
import android.widget.TableLayout;

import com.andreapivetta.game.Player;

public class PlayersTabFragment extends Fragment {


public static final String ARG_SECTION_NUMBER = "section_number";

ScrollView playersScrollView;
TableLayout playerTableScrollView;
Button addButton;

ArrayList<Player> playersList;  
int currentIndex = 0;

public PlayersTabFragment() {

}

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_player_dummy,container, false);

    playersScrollView = (ScrollView) rootView.findViewById(R.id.playersScrollView);
    addButton = (Button) rootView.findViewById(R.id.addButton);
    playerTableScrollView = (TableLayout) rootView.findViewById(R.id.playerTableScrollView);

    addButton.setOnClickListener( new OnClickListener(){

        @Override
        public void onClick(View arg0) {
            insertNewPlayerRow(currentIndex);
            currentIndex++;
        }

    });

    updateSavedPlayers(null);

    return rootView;
}

private void insertNewPlayerRow(int currentIndex) {
    LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View newPlayerRow = inflater.inflate(R.layout.player_row, null);

    EditText playerEditText = (EditText) newPlayerRow.findViewById(R.id.playerEditText);
    ImageButton deletePlayerImageButton = (ImageButton) newPlayerRow.findViewById(R.id.deletePlayerImageButton);

    playerTableScrollView.addView(newPlayerRow,currentIndex);
}
}

我究竟做错了什么?谢谢

编辑:

如果我删除该行:

 android:layout_weight="1"

在包含 scrollView 的 tableRow 中,然后我按下按钮,我可以看到 tableRow 的宽度增加了,但我没有看到里面的 Rows ...

4

0 回答 0