我知道并应用表格来获取屏幕的尺寸,但是失败了,因为它显然在所有分辨率中均无效。例如,如果我为小分辨率定义了一些按钮的位置,则当我以更高的分辨率运行应用程序时,这些按钮会向下滑动。在下图中,我给出了按钮在Emulator 2.7 QVGA API 25(小)和Nexus 7 API 25(xlarge)中如何显示的示例。我是否可以得出结论,Android Studio出现了故障?请帮我看看问题,因为我之前已经搜索并列出了问题,但没有收到正确的答案。 这是我的xml:
<android.support.constraint.ConstraintLayout
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=".MainActivity">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="15"
android:orientation="vertical">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:components="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/layout1">
</RelativeLayout>
</ScrollView>
这是我的java:
public class MainActivity extends AppCompatActivity {
View pulsado;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
//Width and height of screen
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
// Buttons are placed
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout1);
for (int i = 1; i < 5; i++) {
RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
// Size of Buttons
rel_btn.width = 4 * width / 100;
rel_btn.height = 4 * width / 100;
// Position of Buttons
switch (i) {
case 1: // top left
rel_btn.leftMargin = 0;
rel_btn.topMargin = 0;
break;
case 2: // top right
rel_btn.leftMargin = 96 * width / 100;
rel_btn.topMargin = 0;
break;
case 3: // lower left
rel_btn.leftMargin = 0;
rel_btn.topMargin = 80 * height / 100;
break;
case 4: // lower right
rel_btn.leftMargin = 96 * width / 100;
rel_btn.topMargin = 80 * height / 100;
break;
}
Button btnTag = new Button(this);
btnTag.setLayoutParams(rel_btn);
btnTag.setBackgroundColor(Color.BLUE);
btnTag.setId(0 + i);
btnTag.setOnClickListener(prueba);
layout.addView(btnTag);
}
}
private View.OnClickListener prueba = new View.OnClickListener() {
@Override
public void onClick(View view) {
if (pulsado != null) {
Button button1 = (Button) pulsado;
button1.setBackgroundColor(Color.BLUE);
}
Button button2 = (Button) view;
GradientDrawable drawable = new GradientDrawable();
drawable.setShape(GradientDrawable.RECTANGLE);
drawable.setStroke(8, Color.RED);
button2.setBackgroundDrawable(drawable);
pulsado = view;
}
};
}
答案 0 :(得分:1)
您可以尝试在布局文件本身中设置按钮。
Arguments:
C:\Program Files\nodejs\node.exe C:\Users\ganeshk\AppData\Roaming\npm\node_modules\yarn\bin\yarn.js install
PATH:
C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\;C:\Program Files (x86)\Windows Kits\8.0\Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\:C:\Program Files\Java\jdk1.7.0_79\bin;C:\Program Files\Java\jre7\bin;C:\Program Files\TortoiseGit\bin;C:\Program Files\nodejs\;C:\Users\ganeshk\AppData\Roaming\npm
Yarn version:
1.7.0
Node version:
8.11.3
Platform:
win32 x64
Trace:
Error: https://registry.yarnpkg.com/expo: SSL Error: SELF_SIGNED_CERT_IN_CHAIN
at Request.module.exports.Request.onRequestResponse (C:\Users\ganeshk\AppData\Roaming\npm\node_modules\yarn\lib\cli.js:135442:24)
at emitOne (events.js:116:13)
at ClientRequest.emit (events.js:211:7)
at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:543:21)
at HTTPParser.parserOnHeadersComplete (_http_common.js:112:17)
at TLSSocket.socketOnData (_http_client.js:440:20)
at emitOne (events.js:116:13)
at TLSSocket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
at readableAddChunk (_stream_readable.js:250:11)
npm manifest:
{
"name": "CLICK-OMAN",
"version": "0.1.0",
"private": true,
"devDependencies": {
"jest-expo": "25.0.0",
"react-native-scripts": "1.14.0",
"react-test-renderer": "16.3.1"
},
"main": "./node_modules/react-native-scripts/build/bin/crna-entry.js",
"scripts": {
"start": "react-native-scripts start",
"eject": "react-native-scripts eject",
"android": "react-native-scripts android",
"ios": "react-native-scripts ios",
"test": "jest"
},
"jest": {
"preset": "jest-expo"
},
"dependencies": {
"my-local-dep": "link:../../local/path",
"expo": "25.0.0",
"native-base": "^2.6.0",
"react": "16.3.1",
"react-native": "~0.55.2",
"react-native-axios": "^0.17.1",
"react-native-datepicker": "^1.7.2",
"react-native-elements": "^0.19.1",
"react-native-image-header-scroll-view": "^0.8.2",
"react-native-material-bottom-navigation": "^1.0.0",
"react-native-material-dropdown": "^0.11.1",
"react-native-modal": "^6.1.0",
"react-native-modal-selector": "^0.0.27",
"react-native-swiper-flatlist": "^1.0.7",
"react-native-tab-view": "^1.0.2",
"react-navigation": "^2.4.1",
"react-redux": "^5.0.7",
"redux": "^4.0.0",
"redux-form": "^7.4.2",
"redux-persist": "^5.10.0",
"redux-thunk": "^2.3.0",
"remote-redux-devtools": "^0.5.12"
}
}
yarn manifest:
No manifest
Lockfile:
No lockfile