我有一个按钮,我希望显示两种不同的文本大小。我已经将该文本声明为 string.xml 并且我已经在 java 代码中设置了文本,btnSin.setText(getResources().getString(R.string.lblasin) + "\n"
+ getResources().getString(R.string.lblSin));
那么这样做的方法是什么。
每当我使用setOnClickListener方法运行应用程序时,我都会收到错误.否则它工作正常.你能帮我吗?
package com.grozav.meetmeup;
import com.grozav.meetmeup.R;
import com.grozav.meetmeup.library.UserFunctions;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.Button;
public class DashboardActivity extends FragmentActivity implements TabListener {
UserFunctions userFunctions;
ViewPager viewPager;
ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
userFunctions = new UserFunctions();
if (!userFunctions.isUserLoggedIn(getApplicationContext())) {
// user is not logged in show login screen
Intent login = new Intent(getApplicationContext(),
LoginActivity.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); …Run Code Online (Sandbox Code Playgroud) android android-intent android-layout android-fragments android-button
我有一个占据整个屏幕的片段,带有 Buttons 和一个可缩放以适应它的 SeekBar,以及固定大小的 TextViews。我使用带有权重的线性水平和垂直布局来实现这一点。
问题是如果不使按钮扩大大小,我就无法获得足够大的按钮文本。出于某种原因,任何大于 35sp 的文本大小都会使按钮展开,无论按钮有多大。此屏幕截图显示按钮大小有足够的文本空间:

理想情况下,我希望“<”和“>”字符填充按钮。(我打算根据按钮大小以编程方式更改字体大小,例如针对不同的屏幕大小)但还没有尝试过,因为我什至无法让静态布局工作。
编辑:我想避免使用图像,因为如果我有 15 个按钮和 8 个存储桶,那么我需要 120 个图像!
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/VerticalLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp" >
<!-- ........ -->
<TextView
android:id="@+id/trackTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2" >
<Button
android:id="@+id/trackPreviousButton"
style="android:buttonBarStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/button_track_previous"
android:textSize="35sp" />
<Button
android:id="@+id/trackNextButton"
style="android:buttonBarStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/button_track_next"
android:textSize="35sp" />
</LinearLayout>
<SeekBar
android:id="@+id/seekBar"
style="@style/tallerBarStyle"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<!-- ........ -->
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我曾尝试将以下行添加到 Buttons,但它只会产生很小的差异(如果有):
android:padding="0dp"
Run Code Online (Sandbox Code Playgroud)
关于在没有填充的情况下填充按钮的字体高度的建议是我的主要问题。(但是如果可以同时解决动态调整文本大小以填充不同屏幕尺寸的按钮的问题,那就太好了)。
编辑:事实证明,使用较大的字体大小会影响线性布局高度的权重效果,这就是为什么似乎有填充 - 较大的字体大小增加了按钮大小,而不是因为填充(为 0)但因为权重
所以我研究了很多,有很多问题和答案谈论按钮和颜色.但在你投票之前看到这个问题更具体,更实际,我没有找到答案.
我正在使用元素Button作为我的xml布局.来自android.widget的Button类,它扩展了TextView.
此视图可以通过java代码标记为启用或禁用..setEnabled(true|false).
按钮xml代码
<Button
android:id="@+id/maps_list_save_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/str_save"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
Run Code Online (Sandbox Code Playgroud)
我想要的是什么:
我不想做的事:
创建一个新元素并包含布局,我正在避免这个,因为我想保留选定的动画,提升,填充,提升等.再创建一切并不聪明.
我已经尝试过:
更改背景 =它松开内部填充,什么使按钮更大,我想保持材料设计"规则"


更改主题 =我尝试通过编辑器和代码更改主题,但发生了两件事:或者我更改了不是按钮的更多内容,或者我更改了相同颜色的启用和禁用.
即使在寻找文档,我也没有找到如何正确使用这个元素.
android android-theme android-button android-view android-styles
我想在Button上添加呼叫图标,但 drawableTop 不起作用。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp">
<Button
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:drawableTop="@android:drawable/ic_menu_call"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我在我的活动中创建了一个按钮和两个函数/方法,一个是强制退出应用程序的方法,另一个是删除缓存目录的方法。
现在,我想在单击按钮时调用这两种方法,我已取消退出,但我不知道下一步该怎么做。
我的活动代码:
package com.beta.accs;
import android.app.Activity;
import android.content.ComponentName;
import android.os.Bundle;
import android.os.Process;
import android.view.View;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import java.io.File;
import android.content.Context;
import android.widget.Button;
public class MainActivity extends Activity {
public WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.WebViewSNCF);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("https://www.oui.sncf/services/acces-plus"); //Ton url de page Principal;
webView.setWebViewClient(new WebViewClient()); //pour rester dans l'aplication meme en cliquant partout a l'ecran.
webView.getSettings().setBuiltInZoomControls(true);
}
public void clickExit(View view) …Run Code Online (Sandbox Code Playgroud) 我有一个功能正常的按钮,增量和减量功能正常。但我需要我也可以使用 EdidText 手动输入值。
我的代码如下:
活动.tk
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
class MainActivity : AppCompatActivity() {
internal var minteger = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val plus = findViewById<Button>(R.id.increase)
val minus = findViewById<Button>(R.id.decrease)
plus.setOnClickListener {
increaseInteger(plus)
}
minus.setOnClickListener {
decreaseInteger(minus)
}
}
fun increaseInteger(view: View) {
minteger += 1
display(minteger)
}
fun decreaseInteger(view: View) {
minteger -= 1
display(minteger)
}
private fun display(number: Int) {
val displayInteger = findViewById<View>(
R.id.integer_number) as TextView
displayInteger.text …Run Code Online (Sandbox Code Playgroud) 我使用的MaterialButtonToggleGroup是单选(一次只选中一个按钮)。如何检查是否没有选中任何按钮?
toggleGroup?.addOnButtonCheckedListener { group, checkedId, isChecked ->
if (isChecked) {
when (checkedId) {
R.id.first_materialButton -> {
// do something when selected
}
R.id.second_materialButton -> {
// do something when selected
}
}
}
}
Run Code Online (Sandbox Code Playgroud) android android-button android-togglebutton material-components material-components-android
android ×10
android-button ×10
material-components-android ×2
android-view ×1
kotlin ×1
textview ×1
xml ×1