gpt4 book ai didi

java - 在 android 中使用 java 代码的边框宽度和边框颜色?

转载 作者:行者123 更新时间:2023-11-30 02:34:02 25 4
gpt4 key购买 nike

我想在android中使用Java代码在编辑文本中设置边框宽度和边框颜色,我没有使用任何xml文件进行设计,请帮助我是android开发中的新蜜蜂

这是 Activity 课

public class MainActivity extends ActionBarActivity implements OnCheckedChangeListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button myButton = new Button(this);
myButton.setText("ButtonBox4");
myButton.setId(1);
//myButton.setBackgroundColor(Color.YELLOW);
RelativeLayout myLayout = new RelativeLayout(this);
Resources r4 = getResources();
int px4 = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 300, r4.getDisplayMetrics());

myLayout.setMinimumHeight(px4);

Resources r5 = getResources();
int px5 = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 1022, r5.getDisplayMetrics());

myLayout.setMinimumWidth(px5);

RelativeLayout.LayoutParams CheckParams =
new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);

CheckBox checkBox = new CheckBox(this);
checkBox.setOnCheckedChangeListener(this);
checkBox.setId(3);
checkBox.setText("calender6");
CheckParams.addRule(RelativeLayout.ABOVE, myButton.getId());
CheckParams.addRule(RelativeLayout.CENTER_VERTICAL);
CheckParams.setMargins(656, 169, 0, 0);

Resources r6 = getResources();
int px6 = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 40, r6.getDisplayMetrics());

checkBox.setHeight(px6);

Resources r7 = getResources();
int px7 = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 120, r7.getDisplayMetrics());

checkBox.setWidth(px7);

EditText myEditText = new EditText(this);
myEditText.setHint("TextBox2");
myEditText.setId(2);
myEditText.setBackgroundColor(Color.rgb(102, 255, 255));

RelativeLayout.LayoutParams textParams =
new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);

textParams.addRule(RelativeLayout.ABOVE, myButton.getId());
textParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
textParams.setMargins(300, 69,0 ,0);

Resources r = getResources();
int px = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 200, r.getDisplayMetrics());

myEditText.setWidth(px);

Resources r1 = getResources();
int px1 = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 40, r1.getDisplayMetrics());

myEditText.setHeight(px1);

Resources r2 = getResources();
int px2 = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 200, r2.getDisplayMetrics());

myButton.setWidth(px2);

Resources r3 = getResources();
int px3 = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 40, r3.getDisplayMetrics());

myButton.setHeight(px3);

RelativeLayout.LayoutParams buttonParams =
new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
buttonParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
buttonParams.addRule(RelativeLayout.CENTER_VERTICAL);
buttonParams.setMargins(437, 199, 0, 0);

myLayout.addView(myButton, buttonParams);
myLayout.addView(myEditText, textParams);
myLayout.addView(checkBox, CheckParams);
setContentView(myLayout);
}

list 文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dynamicviews"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>


</manifest>

最佳答案

使用形状。例如:

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#ffffff" />
<stroke android:width="1dp" android:color="#4fa5d5"/>
</shape>

在可绘制资源中使用此文本创建 xml,然后将其设置为 TextView 中的背景。

这是你的边框宽度:android:width="1dip"

关于java - 在 android 中使用 java 代码的边框宽度和边框颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26863232/

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