我有一个简单的看法:
<?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:background="@drawable/contact_selected"
android:layout_marginTop="10dp"
android:layout_marginEnd="5dp"
android:orientation="horizontal"
android:padding="3dp">
<TextView
android:id="@+id/txt_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Billy Bob"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
当我将LinearLayout标记静态复制到我的主活动布局时,边距是预期的.但是,当我动态地将视图添加到主活动布局中时,将忽略边距.这是我插入视图的方式
LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.test, null);
TextView txt_title = (TextView)view.findViewById(R.id.txt_title);
txt_title.setText("Dynamic #1");
llayout.addView(view, llayout.getChildCount()-1);
View view2 = inflater.inflate(R.layout.test, null);
txt_title = (TextView)view2.findViewById(R.id.txt_title);
txt_title.setText("Dynamic #2");
llayout.addView(view2, llayout.getChildCount()-1);
Run Code Online (Sandbox Code Playgroud)
这是它的样子:
主布局中的容器是LinearLayout,它是HorizontalScrollView的子级.任何见解都表示赞赏.
我正在尝试创建一个自定义类以显示Yes / No AlertDialog,但我希望onClick处理程序位于实例化该自定义类的活动中。到目前为止,自定义类如下所示:
public class YesNoDialog {
private Context gContext = null;
private DialogInterface.OnClickListener onClickListener;
private AlertDialog alertDialog = null;
public YesNoDialog(Context context,
DialogInterface.OnClickListener listener) {
this.gContext = context;
this.onClickListener = listener;
}
public void ShowDialog() {
AlertDialog.Builder alertDialogBuilder = new
AlertDialog.Builder(this.gContext);
alertDialogBuilder.setTitle("Hello World");
alertDialogBuilder
.setMessage("Are you sure?")
.setCancelable(false)
.setPositiveButton("Yes",this.onClickListener)
.setNegativeButton("No",this.onClickListener);
alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
Run Code Online (Sandbox Code Playgroud)
我的想法是将上下文和onClick处理程序传递给构造函数中的对象,然后将处理程序分配给.setPositive和.setNegative按钮。
我在MainActivity类中实现了DialogInterface.OnClickListener:
public class MainActivity
extends AppCompatActivity
implements DialogInterface.OnClickListener {
Run Code Online (Sandbox Code Playgroud)
并在MainActivity中创建了onClick处理程序,当在对话框中单击“是”或“否”按钮时应调用该处理程序。
@Override
public void onClick(DialogInterface dialog, int id) {
Log.d("DIALOG RETURNS ID=", …
Run Code Online (Sandbox Code Playgroud) 我想让inputType = phone一致地工作.它在一个项目中完美运行,但我不能让它在任何其他项目中工作.通过"工作",我的意思是数字没有格式化为键入的值.这是一个不起作用的测试项目.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.jdot.testphoneinput2.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="@+id/editText"
android:hint="enter phone" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
这是类声明:
package com.jdot.testphoneinput2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Run Code Online (Sandbox Code Playgroud)
笔记:
几乎每篇关于分层小部件主题的文章都指向 FrameLayouts,它应该根据 XML 中的出现顺序进行堆叠。为了测试,我在xml中放了一个按钮和一个TextView,希望能看到TextView与按钮重叠。按钮是放在 textview 之前还是之后都没有关系,按钮总是在顶部。谁能看到我做错了什么?
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.jdot.testloadingfragments.MainActivity">
<Button
android:id="@+id/btn_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text=" "
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="----------------------------------------------------------"/>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)