启动时,我的“活动”屏幕位置会切断我的一半UI

时间:2012-02-02 05:57:08

标签: android android-layout

我是Android开发的新手,我的第一个应用的用户界面遇到了问题。当特定活动启动时,屏幕位置最初会滚动,以便切断我的UI中的顶部视图项目。问题似乎与ExpandableListView和我的实现有关。活动想要最初将它放在屏幕的顶部。如何阻止此滚动发生,以便用户在启动时看到我的活动的顶部?

非常感谢任何帮助,谢谢你们!

屏幕截图:姓名:http://imgur.com/FMba5所需:http://imgur.com/oEx1N

以下是活动的主要布局

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/new_league_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:text="League Settings"
        android:textStyle="bold"
        android:layout_gravity="center_horizontal"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:focusable="true"/>

    <TextView
        android:id="@+id/num_players_prompt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/new_league_title"
        android:text="@string/num_players_prompt"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Spinner
        android:id="@+id/num_players_spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="true"
        android:layout_below="@id/num_players_prompt"
        android:prompt="@string/num_players_prompt" />

    <TextView
        android:id="@+id/roster_size_prompt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/roster_size_prompt"
        android:layout_below="@id/num_players_spinner"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Spinner
        android:id="@+id/roster_size_spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/roster_size_prompt" 
        android:prompt="@string/roster_size_prompt" />

     <ExpandableListView
         android:id="@+id/scoring_settings_list"
         android:layout_width="fill_parent"
         android:layout_height="1000dp"
         android:focusable="false"
         android:layout_below="@+id/roster_size_spinner" />

</RelativeLayout>    

</ScrollView>

这是我用于ExpandedListView的子元素的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >


<EditText
   android:id="@+id/settingEditText"
   android:layout_width="40dp"
   android:layout_height="wrap_content"
   android:inputType="numberDecimal"
   android:maxLength="3"
   android:paddingLeft="6dp"
   android:paddingRight="6dp"
   android:text="6.0" />

<TextView
    android:id="@+id/settingLabel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

这是活动的Java代码

public class NewLeagueActivity extends Activity{

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.new_league);
    setUpUI();

}

public void setUpUI(){

    //Sets up the # of players in league spinner
    Spinner numPlayersSpinner = (Spinner)findViewById(R.id.num_players_spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.num_players, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    numPlayersSpinner.setAdapter(adapter);
    numPlayersSpinner.setSelection(2);

    //Sets up the max roster size spinner
    Spinner rosterSizeSpinner = (Spinner)findViewById(R.id.roster_size_spinner);
    ArrayAdapter adapter2 = ArrayAdapter.createFromResource(this, R.array.roster_size, android.R.layout.simple_spinner_item);
    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    rosterSizeSpinner.setAdapter(adapter2);
    rosterSizeSpinner.setSelection(2);

    //Sets up the expandable listview for Passing settings
    ExpandableListView passScoring = (ExpandableListView)findViewById(R.id.scoring_settings_list);
    passScoring.setAdapter(new EditScoringAdapter());

}

public class EditScoringAdapter extends BaseExpandableListAdapter {
    private String[] groups = { "Score Settings for Passing", "Score Settings for Rushing",
            "Score Settings for Receiving", "Score Settings for Kicking", "Score Settings for Defense", "Score Settings for Special Teams" };
    private String[][] children = {
            { "Points per Passing Touchdown", "Passing Yards per Point", "Points per Interception Thrown" },
            { "Points per Rushing Touchdown", "Rushing Yards per Point", "Points per Fumble" },
            { "Points per Receiving Touchdown", "Receiving Yards per Point", "Points per Reception" },
            { "Points per PAT", "Points per Field Goal (0-39 yds)", "Points per Field Goal (40-49 yds)", "Points per Field Goal (50+ yds)" },
            { "Points per Interception", "Points per Fumble Recovery", "Points per Sack", "Points per Safety", "Points per Defensive Touchdown",
                "Points per Blocked Kick", "Points for Allowing 0 Points", "Points for Allowing 1-6 Points",
                "Points for Allowing 7-13 Points", "Points for Allowing 14-17 Points", "Points for Allowing 18-21 Points",
                "Points for Allowing 22-27 Points", "Points for Allowing 35-45 Points", "Points for Allowing 46+ Points"},
            {}
    };

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return children[groupPosition][childPosition];
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        LayoutInflater inflater = NewLeagueActivity.this.getLayoutInflater();
        View editTextView = inflater.inflate(R.layout.labeled_edit_text, null);
        ((TextView)editTextView.findViewById(R.id.settingLabel)).setText(getChild(groupPosition, childPosition).toString());
        return editTextView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return children[groupPosition].length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups[groupPosition];
    }

    @Override
    public int getGroupCount() {
        return groups.length;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        TextView textView = getGenericView();
        textView.setText(getGroup(groupPosition).toString());
        return textView;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    public TextView getGenericView() {
        //Layout parameters for the ExpandableListView
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, 64);

        TextView textView = new TextView(NewLeagueActivity.this);
        textView.setLayoutParams(lp);
        // Center the text vertically
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        // Set the text starting position
        textView.setPadding(75, 0, 0, 0);
        return textView;

    }

}

}

1 个答案:

答案 0 :(得分:2)

删除ScrollView。将ListView嵌套在ScrollView中和/或反之亦然,这被认为不是一种好习惯。有关详细信息,请参阅this