gpt4 book ai didi

android - 在启动时,我的 Activity 屏幕位置切断了一半的 UI

转载 作者:行者123 更新时间:2023-11-29 22:13:14 25 4
gpt4 key购买 nike

我是 Android 开发的新手,我的第一个应用的 UI 有问题。当一个特定的 Activity 启动时,屏幕位置最初会滚动,以便它切断我 UI 中顶部的几个 View 项目。问题似乎与 ExpandableListView 及其实现有关。 Activity 最初希望将其放置在屏幕顶部。我如何防止这种滚动发生,以便用户在启动时看到我的 Activity 的顶部?

非常感谢任何帮助,谢谢大家!

屏幕截图:初始:http://imgur.com/FMba5所需:http://imgur.com/oEx1N

这是 Activity 的主要布局

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

这是该 Activity 的 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;

}

}

最佳答案

删除 ScrollView。将 ListView 嵌套在 ScrollView 中和/或相反并不是一个好习惯。参见 this更多细节。

关于android - 在启动时,我的 Activity 屏幕位置切断了一半的 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9107800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com