Java Android:如何为2台设备(平板电脑和手机)调用不同的布局
我有一个应用程序,我想在手机和平板电脑上运行。我有两个独立的布局(景观),这两个基本上是相同的,除了他们的尺寸 我的问题是如何让MainActivity.java检测我正在运行的设备,并根据正在使用的设备相应地触发活动 我研究过片段,但我无法真正理解如何创建它们,更不用说如何使用它们 这是我的MainActivity.java:Java Android:如何为2台设备(平板电脑和手机)调用不同的布局,java,android,Java,Android,我有一个应用程序,我想在手机和平板电脑上运行。我有两个独立的布局(景观),这两个基本上是相同的,除了他们的尺寸 我的问题是如何让MainActivity.java检测我正在运行的设备,并根据正在使用的设备相应地触发活动 我研究过片段,但我无法真正理解如何创建它们,更不用说如何使用它们 这是我的MainActivity.java: public class MainActivity extends AppCompatActivity { EditText name; ImageV
public class MainActivity extends AppCompatActivity {
EditText name;
ImageView oneStar, twoStar, threeStar, fourStar, fiveStar;
Intent intent;
FirebaseDatabase rootNode;
DatabaseReference reference;
public void displayScore() {
intent = new Intent(getApplicationContext(), Score.class);
startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); //will hide the title
getSupportActionBar().hide(); // hide the title bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN); //enable full screen
setContentView(R.layout.activity_main);
final FirebaseAnalytics mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
name = findViewById(R.id.editTextTextPersonName);
oneStar = findViewById(R.id.imageView1);
twoStar = findViewById(R.id.imageView2);
threeStar = findViewById(R.id.imageView3);
fourStar = findViewById(R.id.imageView4);
fiveStar = findViewById(R.id.imageView5);
oneStar.setTag(1);
twoStar.setTag(2);
threeStar.setTag(3);
fourStar.setTag(4);
fiveStar.setTag(5);
class OnClickListener implements View.OnClickListener {
@Override
public void onClick(View view) {
Bundle bundle = new Bundle();
bundle.putString(FirebaseAnalytics.Param.VALUE, view.getTag().toString());
if (name.length() == 0){
name.setError("Please enter your full name.");
}
else {
name.setError(null);
rootNode = FirebaseDatabase.getInstance();
reference = rootNode.getReference().child("Users");
//Fetch all values
String username = name.getText().toString();
String value = view.getTag().toString();
int rating = Integer.parseInt(value);
UserHelper helper = new UserHelper(username,rating);
reference.push().setValue(helper);
Toast.makeText(MainActivity.this, "Feedback submitted successfully!", Toast.LENGTH_SHORT).show();
sleep(2500);
displayScore();
}
}
}
oneStar.setOnClickListener(new OnClickListener ());
twoStar.setOnClickListener(new OnClickListener ());
threeStar.setOnClickListener(new OnClickListener ());
fourStar.setOnClickListener(new OnClickListener ());
fiveStar.setOnClickListener(new OnClickListener ());
}
}
任何帮助都将不胜感激
提前感谢。您不应该看到平板电脑和手机的两种不同活动。有不同的资源布局文件夹可用于此目的。您可以在
布局栏
中创建横向布局,您的肖像就在普通布局文件夹中。然后您只有一个活动,操作系统根据屏幕和方向选择要使用的布局您的布局应该灵活,以适应不同的大小。所有尺寸都应该只有一个布局,避免硬编码尺寸。
这是一篇文章的摘录
对不起,我的问题用错了词。我有两个布局文件和一个MainActivity.java。这两个
layout.xml
都在我的layout
文件夹下。但是操作系统似乎无法识别我正在使用的设备(我的手机的布局都乱七八糟。)@ArpitMundra正如这个答案所暗示的,您需要将布局文件组织在正确的文件夹中,以便系统根据设备的方向、分辨率等自动识别它们。有关详细信息,请参阅:
"Avoid hard-coded layout sizes. To ensure that your layout is flexible and adapts to different screen sizes, you should use "wrap_content" and "match_parent" for the width and height of most view components, instead of hard-coded sizes.
"wrap_content" tells the view to set its size to whatever is necessary to fit the content within that view.
"match_parent" makes the view expand to as much as possible within the parent view."